• Automatic Reference Counting (ARC) • Safely Typed • Semantically, Strong + Static Typing • Strict type interfaces with subtype polymorphism • Consistent Semantics around Mutability • Much of the language built in itself 7 implementation inheritance
be used when a variable might not contain a value, i.e. should be settable to `nil`. • Optionals are represented as a Type followed by `?` • e.g.: String? Optionals
be unwrapped to access their values. if (lastAttemptedLaunchCode != nil) { var code = lastAttemptedLaunchCode! println("Launch Code Entered: \(code)") } Declare an optional variable var lastAttemptedLaunchCode: String? // initialized to nil lastAttemptedLaunchCode = "123456" // {Some "123456"} explicit unwrapping
statement to conditionally and implicitly unwrapped and assign its value if let code = lastAttemptedLaunchCode { println("Launch Code Entered: \(code)") }
switch rgb { case (0, 255, 0): println("green") case (255, 255, _) println("shade of yellow") case (let r, 0, 0): println("Just red with a value of \(r)") case let (r, g, b) where r == g && g == b: println("shade of grey") default: println("Some other color") }
an argument func iterate(value: Int, iterations: Int,fn: ((Int) -> Int)) -> Int { var acc = value for _ in 0 ..< iterations { acc = fn(acc) } return acc } Pass a Function as an argument iterate(50, 25, incrementByTwo) // 100
4, 3] Double each value using map let doubled = quuux.map({n in n * 2}) // [2, 4, 10, 8, 6] Get only even Ints using filter let evens = quuux.filter({$0 % 2 == 0}) // [2, 4] Get the sum of the Ints in the array using reduce let sum = quuux.reduce(0, +) // 15
with a raw type of Int enum HTTPCode: Int { case OK = 200 case Created = 201 case Accepted = 202 case MovedPermanently = 301 case BadRequest = 400 case Unauthorized = 401 case NotFound = 404 case IAmATeapot = 418 }
an Enum value to its raw representation let responseDict = ["status": HTTPCode.IAmATeapot.toRaw(), "body": "short and stout"] Note: Because I created a Dictionary with multiple keyval types, Swift's type inference inferred it as having a type [String: AnyObject?] Enumeration with a raw type of Int enum HTTPCode: Int { case OK = 200 case Created = 201 case Accepted = 202 case MovedPermanently = 301 case BadRequest = 400 case Unauthorized = 401 case NotFound = 404 case IAmATeapot = 418 }
let code = responseCode { switch code { case .OK, .Created, .Accepted: println("Success! ") case .BadRequest, .Unauthorized, .NotFound: println("Something went wrong, ") case .IAmATeapot: println("Wtf? ") default: println("Something else happened... ") } } Creating an Enum form its raw representation (always returns an optional) let responseCode: HTTPCode? = HTTPCode.fromRaw(418) Enumeration with a raw type of Int enum HTTPCode: Int { case OK = 200 case Created = 201 case Accepted = 202 case MovedPermanently = 301 case BadRequest = 400 case Unauthorized = 401 case NotFound = 404 case IAmATeapot = 418 }
passed by value) • Classes are Reference Types • Structure and Class methods *are* functions that close over the Structure or Class instance variables • Fundamental Types in Swift are built from Structures (e.g., Arrays, Dictionaries, etc.)
value: Int let maxHistoryLength: Int var history: [Int] } If you don't provide an initializer, a default initializer is generated with named parameters let myHistInt = HistInt(value: 3, maxHistoryLength: 5, history: []) Note: This is an immutable struct (i.e., cannot set `value`, because the HistInt instance is assigned to a constant)
an array of type [ListItem] var myListItems:[ListItem] = [myTask, myBug] Iterate over ListItems for item in myListItems { println(item.name) } // Swift Presentation // Slide code compilation error
Optionals let myThings = ["bugs": [myBug]] if let myBugs = myThings["bugs"] { if let bug = myBugs.first { if let relatedTask = bug.relatedTask { if let dueDate = relatedTask.dueDate { println("\(dueDate)") } } } }