Slide 1

Slide 1 text

Ash Furrow, Artsy Lessons from Production Swift

Slide 2

Slide 2 text

February, 2014

Slide 3

Slide 3 text

Swift Let’s talk about

Slide 4

Slide 4 text

1. The Need for Swift 2. The Fast and the Faulty 3. Seek Out Bold New Worlds

Slide 5

Slide 5 text

Need Swift The for

Slide 6

Slide 6 text

Objective-C

Slide 7

Slide 7 text

TXS ;push return address back onto stack LDY #$00 ;load Y with zero - will hold result LOOP1 LDAA TEMP ;load A into TEMP ANDA MASK ;AND A with Mask BNE ADD1 BRA NONE1 ADD1 INY ;increment our counter of 1's NONE1 LDAA TEMP ;reload accumulator A LSL MASK ;Shift the mask's 1 bit left BNE LOOP1 ;If we haven't finished our loop, branch LDAA #$01 ;load new mask into A STAA MASK ;store the reset mask into MASK TSX ;pull of return address and store in X PULA ;pull off A STAA TEMP ;store the value into temp TXS ;push return address back onto the stack LOOP2 LDAA TEMP ;Load A into TEMP ANDA MASK ;logical AND MASK with A BNE ADD2 ;add one if we need to BRA NONE2 ADD2 INY ;increment our counter of 1's NONE2 LDAA TEMP LSL MASK ;shift our mask left by one BNE LOOP2 ;loop back until we've exhausted positions STY TEMP ;store Y into TEMP - this is the number of 1's LDAB TEMP ;load the number of 1's into B LDAA #$10 ;load dec 16 into A SBA ;A - B -> A same as 16 - B -> A

Slide 8

Slide 8 text

—John Siracusa “While hardware performance increases over time, the human capacity to deal with complexity does not.”

Slide 9

Slide 9 text

Stuck Objective-C Past is in the

Slide 10

Slide 10 text

Beyond Hope Objective-C is

Slide 11

Slide 11 text

Criteria 1. No C baggage 2. Memory Managed 3. Native unicode strings 4. Native collections 5. Be concise 6. Named Parameters

Slide 12

Slide 12 text

Criteria 1. No C baggage 2. Memory Managed 3. Native unicode strings 4. Native collections 5. Be concise 6. Named Parameters

Slide 13

Slide 13 text

The Need for Swift • Programming abstraction increases over time • Objective-C has improved all it can • Swift is the next step • Revolution, not evolution

Slide 14

Slide 14 text

Fast Faulty The and the

Slide 15

Slide 15 text

Problems In Beta • Xcode crashes • Compiler segfaults • Constant changes to Swift • Many language limitations

Slide 16

Slide 16 text

Problems Now • Still some stability issues • Still frequent changes to Swift • Still some remaining language limitations

Slide 17

Slide 17 text

Problems Now • Language changes tied to Xcode versions • Runtime changes tied to Xcode versions • Changes in resource-loading in frameworks

Slide 18

Slide 18 text

Community Tools • Most still in their infancy • CocoaPods, Jazzy, SBConstants… • The rest do not exist (or are still in beta) • Test coverage analyzer

Slide 19

Slide 19 text

Community Libraries • Struggling to accomodate frequent Swift changes • Xcode updates make this difficult

Slide 20

Slide 20 text

The Fast and the Faulty • Swift had a rough start • Was to be expected • Things are better now • Mostly • Growing pains

Slide 21

Slide 21 text

Seek Out New Worlds Bold

Slide 22

Slide 22 text

Problem Solving in Objective-C Problem Solving in Swift is not

Slide 23

Slide 23 text

Examples

Slide 24

Slide 24 text

Index Paths • Used to identify cells in a table view • Section, row • Lots of horrendous code • It’s so bad • Seriously bad

Slide 25

Slide 25 text

Index Paths if (indexPath.section == 0) { if (indexPath.row == 0) { } else if (indexPath.row == 1) { 
 } else if ... } else if (indexPath.section == 1) { if (indexPath.row == 0) { } else if (indexPath.row == 1) { 
 } else if ... } else if ...

Slide 26

Slide 26 text

Index Paths switch (indexPath.section, indexPath.row) { case (0, 0): case (0, 1): case (1, 0): case (1, 1): default: // nop }

Slide 27

Slide 27 text

Still Terrible This is

Slide 28

Slide 28 text

Index Paths switch (indexPath.section, indexPath.row) { case (0, let row): // Executed for any section 0, row is row case (let section, 0) where section % 2 == 1: // Executed for first rows of odd all sections case (let section, let row) where validate(section): // Executed when validate() returns true default: // Executed on all other cases }

Slide 29

Slide 29 text

Generics • Define functions, structs, others in the abstract • Create one instead of many • Arrays, dictionaries, and sets are all generics

Slide 30

Slide 30 text

Generics struct Stack { private var contents = Array() mutating func push(value: T) { contents.insert(value, atIndex: 0) } mutating func pop() -> T { return contents.removeAtIndex(0) } var isEmpty: Bool { return countElements(contents) == 0 } }

Slide 31

Slide 31 text

Generics var intStack = Stack() var stringStack = Stack() var stackStack = Stack>() intStack.push(1) intStack.pop() // Returns 1

Slide 32

Slide 32 text

Lazy Loading • Create resource when it is first accessed • Avoids unnecessary work for CPU • Avoids unnecessary memory use

Slide 33

Slide 33 text

Lazy Objective-C • No language-level support • Lots of repetition • Tedious work • Lots of repetition

Slide 34

Slide 34 text

Lazy Objective-C @interface MyClass: NSObject @property (nonatomic, copy) NSString *name; @end @implementation - (NSString *)name { if (_name == nil) { _name = "Ash Furrow"; } return _name; } @end

Slide 35

Slide 35 text

Lazy Swift class MyClass { lazy var name = "Ash Furrow" }

Slide 36

Slide 36 text

Lazy Swift class MyClass { lazy var name = "Ash Furrow" } MyClass().name // Returns "Ash Furrow" let instance = MyClass() instance.name = "Orta Therox" instance.name // Returns “Orta Therox"

Slide 37

Slide 37 text

Lazy Swift class MyClass { lazy var name = "Ash Furrow” lazy var greeting: String = { return "Hello, \(self.name)" }() } MyClass().greeting // Returns "Hello, Ash Furrow" let instance = MyClass() instance.name = "Orta Therox" instance.greeting // Returns "Hello, Orta Therox" instance.name = "Eloy Durán" instance.greeting // Returns "Hello, Orta Therox"

Slide 38

Slide 38 text

Extending Types • Objective-C has “categories” for extending existing classes • Swift has “extensions” instead • They work on all types

Slide 39

Slide 39 text

Extending Types extension Int { func times(closure: () -> ()) { for i in 0..

Slide 40

Slide 40 text

Extending Types extension Int { var hours: NSTimeInterval { return NSTimeInterval(3600 * self) } } extension NSTimeInterval { var fromNow: NSDate { return NSDate(timeIntervalSinceNow: self) } var ago: NSDate { NSDate(timeIntervalSinceNow: -self) } } 4.hours.fromNow 4.hours.ago

Slide 41

Slide 41 text

Functional Awesomeness let people = ["Carter", "Sebastian", "Daniel"] people.map { "Hello, \($0)!" } func hello(name: String) -> String { return "Hello, \(name)!" } people.map(hello) or…

Slide 42

Slide 42 text

Functional Awesomeness func say(greeting: String) -> (name: String) -> String { return { (name: String) -> String in "(greeting), \(name)!" } } people.map(say("Hello")) people.map(say("Merhaba"))

Slide 43

Slide 43 text

Functional Awesomeness func say(greeting: String)(name: String) -> String { return "(greeting), \(name)!" } people.map(say("Hello")) people.map(say("Merhaba"))

Slide 44

Slide 44 text

SBConstants • Generate constants for Storyboard identifiers • Compile-time checked enum • Can be extended…

Slide 45

Slide 45 text

SBConstants func performSegue(identifier: SegueIdentifier) { performSegueWithIdentifier(identifier.rawValue, sender: self) } ... func ==(lhs: UIStoryboardSegue, rhs: SegueIdentifier) -> Bool { return lhs.identifier == rhs.rawValue }

Slide 46

Slide 46 text

SBConstants peformSegue(.RegisterCreditCard) ... func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue == .RegisterCreditCard { ... } }

Slide 47

Slide 47 text

SBConstants • Don’t use rawValue all over the place • Abstract it away • Get compile-time safety

Slide 48

Slide 48 text

Familiar Problems New Ways to solve Let’s look for

Slide 49

Slide 49 text

For Advice Ask other communities Let’s

Slide 50

Slide 50 text

Resources • Natasha the Robot’s newsletter • iOSDevWeekly • iOS Goodies • GitHub Explore • leanpub.com/yourfirstswiftapp

Slide 51

Slide 51 text

Eidolon

Slide 52

Slide 52 text

1. The Need for Swift 2. The Fast and the Faulty 3. Seek Out Bold New Worlds

Slide 53

Slide 53 text

Better Mistakes Make Tomorrow