Slide 1

Slide 1 text

WELCOME TO OUR 1ST MEETUP Organizer Simon Ng / Sean Choo Venue Sponsor City U Apps Lab Meetup URL http://meetup.com/swifthk

Slide 2

Slide 2 text

ABOUT ME ▸ Founder of AppCoda ▸ iOS Developer and Blogger ▸ Author of Beginning iOS 9 Programming with Swift

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

ABOUT THIS TALK 1. A Quick introduction to Swift 2. Using Xcode 7 and Playgrounds 3. Building a Simple App with Interface Builder

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

DEVELOPERS USE OBJECTIVE-C

Slide 8

Slide 8 text

LEARN SWIFT OR OBJECTIVE-C?

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

OBJECTIVE-C VS SWIFT // Objective-C has header files #import #import "CustomTableViewController.h" #import "CustomTableViewCell.h" @interface CustomTableViewController () @end @implementation CustomTableViewController { } // Swift has no header files import UIKit class CustomTableViewController: UITableViewController { }

Slide 11

Slide 11 text

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"

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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.

Slide 14

Slide 14 text

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!"

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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"]

Slide 18

Slide 18 text

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)") }

Slide 19

Slide 19 text

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") }

Slide 20

Slide 20 text

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"

Slide 21

Slide 21 text

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"

Slide 22

Slide 22 text

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())

Slide 23

Slide 23 text

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"

Slide 24

Slide 24 text

YOU USE OPTIONALS IN SITUATIONS WHERE A VALUE MAY BE ABSENT.

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

var name = "Simon Ng" var email = "[email protected]" var phone:String? = nil var howDidYouHear:String? = nil

Slide 27

Slide 27 text

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?

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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.

Slide 30

Slide 30 text

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.

Slide 31

Slide 31 text

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.

Slide 32

Slide 32 text

PLAYGROUNDS DEMO 2 2 The source code is available on GitHub: https://github.com/simonng/PlaygroundDemo

Slide 33

Slide 33 text

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