Variability and Congestive Heart Failure” Then: Now: Mobile and Web Business Apps Atlanta-ish, GA @pxtrick Patrick Seda Increasing the Quality of the Universe
simple single-target app, the code in your app is typically self-contained within the app and doesn’t need to be made available outside of the app’s module. The default access level of internal already matches this requirement. Therefore, you don’t need to specify a custom access level.” Because: Less typing for small apps! Access Control @pxtrick
enum AlarmState { case disarmed case armed case alarming } // System-wide events. enum Event { case arm case disarm case tripSensor } Named Model Types @pxtrick
{ case .arm: if (currentState == .disarmed) { currentState = .armed } case .disarm: currentState = .disarmed case .tripSensor: if (currentState == .armed) { currentState = .alarming } } } Named Model Types @pxtrick
case lesPaul case es355 case flyingV case sg } let myGuitar = Gibson.lesPaul print("My guitar is a \(myGuitar.rawValue)") let yourGuitar: Gibson = .flyingV print("Your guitar is a \(yourGuitar.rawValue)") My guitar is a lesPaul Your guitar is a flyingV Named Model Types @pxtrick Ou t p u t :
case lesPaul = "Les Paul" case es355 = "ES355" case flyingV = "Flying V" case sg = "SG" } let myGuitar = Gibson.lesPaul print("My guitar is a \(myGuitar.rawValue)") let yourGuitar: Gibson = .flyingV print("Your guitar is a \(yourGuitar.rawValue)") My guitar is a Les Paul Your guitar is a Flying V Named Model Types @pxtrick Ou t p u t :
cases, define a class, and create instances of that class to be managed and passed by reference. In practice, this means that most custom data constructs should be classes, not structures.” Use a struct when: - Encapsulate few, simple data values - Data can be copied (passed by value) - Does not need inheritance Named Model Types @pxtrick
} } let cello: MusicalInstrument = Cello() let bass: MusicalInstrument = Bass() let musician = Musician() musician.instrument = cello musician.perform() musician.instrument = bass musician.perform() Hurrm Hummm A Boom Boom Boom Protocols for Design @pxtrick Ou t p u t :
value: inout Int, onDone onDone: () -> Void) { value += 1 onDone() } // Define our callback. var attentionCb = { print("Attention! value = \(value)") } // Test it out. var value = 3; increment(&value, onDone: attentionCb) increment(&value, onDone: attentionCb) Attention! value = 4 Attention! value = 5 @pxtrick Ou t p u t : Closures - C a l lb a cks
func execute(_ work: () -> Void) { queue.sync { work() } } // Create some work. var workItemOne = { print("Work item 1") } var workItemTwo = { print("Work item 2") } execute(workItemOne) execute(workItemTwo) Work item 1 Work item 2 @pxtrick Ou t p u t : Closures - T i m e of E xecu t ion
func execute(_ work: () -> Void) { queue.async { work() } } // Create some work. var workItemOne = { print("Work item 1") } var workItemTwo = { print("Work item 2") } execute(workItemOne) execute(workItemTwo) error: closure use of non-escaping parameter 'work' may allow it to escape @pxtrick Ou t p u t : Closures - T i m e of E xecu t ion
func execute(_ work: @escaping () -> Void) { queue.async { work() } } // Create some work. var workItemOne = { print("Work item 1") } var workItemTwo = { print("Work item 2") } execute(workItemOne) execute(workItemTwo) Work item 2 Work item 1 @pxtrick Ou t p u t : Closures - T i m e of E xecu t ion
a normal comparison function. func backwards(_ s1: String, _ s2: String) { return s1 > s2 } // Execute the array sort. var reversedOrder = cities.sorted(by: backwards) print("\(reversedOrder)") ["Traer", "Senoia", "Mint Hill", "Geneva"] @pxtrick Ou t p u t : Closures - C l os u r e E xp r e s s io n s
Expression syntax. var reversedOrder = cities.sorted(by: { (s1: String, s2: String) -> Bool in return s1 > s2 }) ["Traer", "Senoia", "Mint Hill", "Geneva"] @pxtrick Ou t p u t : Closures - C l os u r e E xp r e s s io n s
type from context. var reversedOrder = cities.sorted(by: { s1, s2 in return s1 > s2 }) ["Traer", "Senoia", "Mint Hill", "Geneva"] @pxtrick Ou t p u t : Closures - C l os u r e E xp r e s s io n s
returns from single-expression closures. var reversedOrder = cities.sorted(by: { s1, s2 in s1 > s2 }) ["Traer", "Senoia", "Mint Hill", "Geneva"] @pxtrick Ou t p u t : Closures - C l os u r e E xp r e s s io n s
argument names. var reversedOrder = cities.sorted(by: { $0 > $1 }) ["Traer", "Senoia", "Mint Hill", "Geneva"] @pxtrick Ou t p u t : Closures - C l os u r e E xp r e s s io n s
methods. var reversedOrder = cities.sorted(by: >) ["Traer", "Senoia", "Mint Hill", "Geneva"] @pxtrick Ou t p u t : Closures - C l os u r e E xp r e s s io n s
methods. var reversedOrder = cities.sorted(by: >) ["Traer", "Senoia", "Mint Hill", "Geneva"] @pxtrick Ou t p u t : // Closure Expression syntax. var reversedOrder = cities.sorted(by: { (s1: String, s2: String) -> Bool in return s1 > s2 }) Previously: Closures - C l os u r e E xp r e s s io n s
Create our keys. var keys: Keyboard? = Keyboard() // Establish a beautiful friendship. band?.keys = keys keys?.band = band // Clean up. print("Removing band ...") band = nil print("Removing keys ...") keys = nil Removing band ... Removing keys ... Memory Leaks @pxtrick Ou t p u t :
Create our keys. var keys: Keyboard? = Keyboard() // Establish a beautiful friendship. band?.keys = keys keys?.band = band // Clean up. print("Removing band ...") band = nil print("Removing keys ...") keys = nil Removing band ... [RockBand] - deinit() Removing keys ... [Keyboard] - deinit() Memory Leaks @pxtrick Ou t p u t :
// Create and assign our fans. rockShow?.fans = Audience(rockShow!) // Clean up. print("Removing rockShow...") rockShow = nil Removing rockShow ... Memory Leaks @pxtrick Ou t p u t :