About Swift ● Safety ● Support Cocoa and Cocoa Touch ● Works side by side with Objective-C code ● Single implementation file ● Error handling, closures, multiple return values, functional programming patterns, generics, fast iteration etc. etc. etc.
Variables and Constants var meetupName = “CocoaHeads” // String var version = 3 // Int let meetupCity = “Athens” // String let isSummer = true // Bool let π = 3.14 // Double let = " "
Optionals You use optionals in situations where a value may be absent. An optional says: ● There is a value, and it equals x ● There isn’t a value at all ● Only
Optionals ● The concept of optionals doesn’t exist in C or Objective-C. The nearest thing in Objective-C is the ability to return nil ● However, this only works for objects - it doesn’t work for structures, basic C types, or enumeration values. Requires NSNotFound or something. ● Optionals in Swift work for everything
Optionals var vaders: Int? = forceAwakensCharacters[“vader”] if(vaders == nil) { // do something } else { let count: Int = vaders! } if(vadersPhones) { let count: Int = vaders! }
Optionals let cast = ["luke": 1, "wookie": 2] var vader : Int? = cast["vader"] if let vader = cast["vader"] { // Optional Binding } guard let vader = cast["vader"] else { break } var vader2: Int = cast["vader"]!
Optionals Note: Trying to use ! to access a non-existent optional value triggers a runtime error. Always make sure that an optional contains a non-nil value before using ! to force-unwrap its value.
Tuples ● Tuples group multiple values into a single compound value ● The values within a tuple can be of any type and do not have to be of the same type as each other.
Tuples let http404Error = (404, "Not Found") // (Int, String) let (statusCode, statusMessage) = http404Error print("The status code is \(statusCode)") // prints "The status code is 404" print("The status message is \(statusMessage)") // prints "The status message is Not Found”
Tuples let http200Status = (statusCode: 200, description: "OK") print("The status code is \(http200Status.statusCode)") // prints "The status code is 200" print("The status message is \(http200Status.description)") // prints "The status message is OK
Closures ● Closures are self-contained blocks of functionality that can be passed around and used in your code. ● Closures in Swift are similar to blocks in C and Objective-C and to lambdas in other programming languages.
Property Observers class Cat { init() { mouth = “Big” } var mouth : String { willSet { // old value available here } didSet { // new value available here } }
Error handling You use error handling to respond to error conditions your program may encounter during execution ● Determine the underlying cause of failure unlike optionals which only communicate the presence of a failure
Error handling func canThrowAnError() throws { // this function may or may not throw an error } do { try canThrowAnError() // no error was thrown } catch { // an error was thrown }
Getting started Interesting cocoa free repos Epoch // HTTP server for Swift 2.2 on Linux Coolie // Coolie helps you to create models (& their constructors) from JSON file Style guide