Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Getting Started with Swift and iOS Programming

Simon Ng
February 18, 2016

Getting Started with Swift and iOS Programming

The first tech talk of the Swift Hong Kong Meetup group. In this presentation, Simon gives a quick introduction to Swift and Playgrounds.

For the source code of the Playgrounds demo, please check it out on GitHub.

https://github.com/simonng/PlaygroundDemo

---
Swift Hong Kong is a meetup group, devoted to Swift and iOS Development. Each month we will gather to talk about Swift, latest trends, frameworks, third-party libraries, and tools. All experience levels, whether you’re a beginner or a mobile developers with years of experience, are welcome. If you want to learn Swift and are interested in iOS app development, this group is for you. For details, you can check it out here:

http://www.meetup.com/swifthk

Simon Ng

February 18, 2016
Tweet

More Decks by Simon Ng

Other Decks in Programming

Transcript

  1. WELCOME TO OUR 1ST MEETUP Organizer Simon Ng / Sean

    Choo Venue Sponsor City U Apps Lab Meetup URL http://meetup.com/swifthk
  2. ABOUT ME ▸ Founder of AppCoda ▸ iOS Developer and

    Blogger ▸ Author of Beginning iOS 9 Programming with Swift
  3. ABOUT SWIFT HONG KONG 1. All about Swift and iOS/tvOS/watchOS

    Development 2. Gather together for knowledge exchange 3. Talk about your iOS Projects, ideas, etc
  4. ABOUT THIS TALK 1. A Quick introduction to Swift 2.

    Using Xcode 7 and Playgrounds 3. Building a Simple App with Interface Builder
  5. SWIFT ▸ A new programming language for iOS, OS X,

    watchOS, and tvOS apps ▸ Swift 1.0 was first released in Sep, 2014. ▸ Swift is now open source (swift.org) ▸ Safe, modern and cleaner syntax ▸ Friendly to new programmers
  6. DEVELOPERS LOVE SWIFT 1 1 In reference to the developer

    survey of StackOverflow, which was run in Feb, 2015. For more details, you can check out the full results at http://stackoverflow.com/research/developer-survey-2015
  7. OBJECTIVE-C VS SWIFT // Objective-C const int count = 10;

    double price = 23.55; NSString *firstMessage = @"Swift is awesome. "; NSString *secondMessage = @"What do you think?"; NSString *message = [NSString stringWithFormat:@"%@%@", firstMessage, secondMessage]; // Swift let count = 10 var price = 23.55 let firstMessage = "Swift is awesome." let secondMessage= "What do you think?" var message = firstMessage + secondMessage
  8. OBJECTIVE-C VS SWIFT // Objective-C has header files #import <UIKit/UIKit.h>

    #import "CustomTableViewController.h" #import "CustomTableViewCell.h" @interface CustomTableViewController () @end @implementation CustomTableViewController { } // Swift has no header files import UIKit class CustomTableViewController: UITableViewController { }
  9. VARIABLES AND CONSTANTS // To declare a variable, you use

    the var keyword var number: Int = 10 // To declare a constant, you use the let keyword let maxNumber: Int = 100 maxNumber = 50 // Error! // Swift is a type-safe language. // A type safe language encourages you // to be clear about the types of values. var radius: Double = 25.75 let isListening: Bool = true let message: String = "Welcome"
  10. TYPE INFERENCE var number = 10 // Inferred as Integer

    let maxNumber = 100 // Inferred as Integer var radius = 25.75 // Inferred as Double let isListening = true // Inferred as Bool let message = "Welcome" // Inferred as String
  11. STRING BASICS // Declare a String let message = "Welcome"

    let dinner = "犡ภ觬!"or#$%or&'?" let ! = "hamburger" // String concatenation var newMessage = message + " " + ! // Welcome hamburger // String interpolation let price = 20.0 newMessage = "The price of a \(!) is $\(price)." // The price of a hamburger is $20.
  12. STRING COMPARISON MADE SIMPLE let message1 = "Hello!" let message2

    = "Hi!" // Simply use the == operator to compare Strings if message1 == message2 { print("Same!") } else { print("Different!") } // prints "Same!"
  13. ARRAYS let recipes:[String] = ["Egg Benedict", "Mushroom Risotto", "Hamburger"] let

    count:[Int] = [3, 4, 19, 30] // Type Inference let recipes = ["Egg Benedict", "Mushroom Risotto", "Hamburger"] let count = [3, 4, 19, 30] // Type Safety - it can only hold elements of the same type let recipes:[String] = ["Egg Benedict", "Mushroom Risotto", "Hamburger", 3] // Error because 3 is an Integer
  14. ARRAY MANIPULATION var cities = [String]() // Empty array cities

    += ["Hong Kong"] // ["Hong Kong"] cities.append("Paris") // ["Hong Kong", "Paris"] cities += ["London", "Sydney"] // ["Hong Kong", "Paris", "London", "Sydney"] cities[0] = ["Osaka"] // ["Osaka", "Paris", "London", "Sydney"] cities[1...2] = ["Florence", "Taipei"] // ["Osaka", "Florence", "Taipei", "Sydney"]
  15. DICTIONARIES var exchangeRate = [String: Double]() exchangeRate["HKD"] = 7.79 exchangeRate["AUD"]

    = 1.41 exchangeRate["EUR"] = 0.89 exchangeRate["JPY"] = 113.22 for (currency, rate) in exchangeRate { print("\(currency) = \(rate)") }
  16. SWITCH WITH STRINGS let recipe = "Egg Benedict" switch recipe

    { case "Egg Benedict": print("Let's cook!") case "Mushroom Risotto": print("Hmm... let me think about it") case "Hamburger": break default: print("Anything else") }
  17. MULTIPLE MATCHES IN A SINGLE CASE let symbol = "✌"

    switch symbol { case "one", "1", "Ӟ", """: print("One") case "two", "2", "ԫ", "#", "✌": print("Two") case "eight", "8", "獌", "$", "%": print("Eight") default: break } // prints "Two"
  18. SWITCH WITH RANGE var speed = 50 switch speed {

    case 0: print("stop") case 1...40: print("slow") case 41...70: print("normal") case 71..<101: print("fast") default: print("very fast") } // prints "normal"
  19. CLASSES AND METHODS class ShoppingCart { var items:[String] = [String]()

    func addItem(item: String) { items.append(item) } func getItemTotal() -> Int { return items.count } } let shoppingCart = ShoppingCart() shoppingCart.addItem("Apple Watch") print(shoppingCart.getItemTotal())
  20. OPTIONALS var exchangeRate = [String: Double]() exchangeRate["HKD"] = 7.79 exchangeRate["AUD"]

    = 1.41 exchangeRate["EUR"] = 0.89 exchangeRate["JPY"] = 113.22 print(exchangeRate["HKD"]) // prints "Optional(7.79)\n"
  21. var name = "Simon Ng" var email = "[email protected]" var

    phone:String? = nil var howDidYouHear:String? = nil
  22. DECLARING AN OPTIONAL By adding a ?, you tell the

    compiler the variable is an optional. Here you define an optional variable with a default value. var message? = "I'm an optional" Without a default value, the variable is set to nil. var message:String?
  23. WHY OPTIONALS Objective-C NSString *howDidYouHear = @"Facebook"; NSString *message =

    [@"Answer: " stringByAppendingString:howDidYouHear]; // prints "Answer: Facebook" NSString *howDidYouHear = nil; NSString *message = [@"Answer: " stringByAppendingString:howDidYouHear]; // Error! Swift
  24. FORCED UNWRAPPING AN OPTIONAL var howDidYouHear:String? = "Facebook" if howDidYouHear

    != nil { let message = "Answer: " + howDidYouHear! } You can access its underlying value by adding an exclamation mark (!) to the end of the optional's name.
  25. OPTIONAL BINDING var howDidYouHear:String? = "Facebook" if let answer =

    howDidYouHear { let message = "Answer: " + answer } You use if let in Swift to check if an optional holds a value, and in case it does, extract that value into a constant. Alternatively, use if var if you want to extract the value into a variable.
  26. EXTENSIONS extension String { func printCharacters() { for character in

    self.characters { print(character) } } } let message = "Swift is awesome !" message.printCharacters() You can easily add new functionality to an existing class, structure, enumeration, or protocol type by using extensions.
  27. PLAYGROUNDS DEMO 2 2 The source code is available on

    GitHub: https://github.com/simonng/PlaygroundDemo
  28. THANK YOU ▸ Apple's Swift Programming Reference (https:// itunes.apple.com/us/book/swift-programming- language/id881256329?mt=11)

    ▸ AppCoda (http://www.appcoda.com) ▸ Got questions? You can contact me at [email protected] or https://facebook.com/ appcodamobile