Slide 1

Slide 1 text

Swift Style @gregheo

Slide 2

Slide 2 text

Style?

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

for _ in 0..

Slide 6

Slide 6 text

for _ in 0..

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

MOV AX, SI MOV CX, BX DEC CX @OUTER_LOOP: MOV BX, CX MOV SI, AX MOV DI, AX INC DI @INNER_LOOP: MOV DL, [SI] CMP DL, [DI] JNG @SKIP_EXCHANGE XCHG DL, [DI] MOV [SI], DL @SKIP_EXCHANGE: INC SI INC DI DEC BX JNZ @INNER_LOOP LOOP @OUTER_LOOP MOV AX, SI ; set AX=SI MOV CX, BX ; set CX=BX DEC CX ; set CX=CX-1 @OUTER_LOOP: ; loop label MOV BX, CX ; set BX=CX MOV SI, AX ; set SI=AX MOV DI, AX ; set DI=AX INC DI ; set DI=DI+1 @INNER_LOOP: ; loop label MOV DL, [SI] ; set DL=[SI] CMP DL, [DI] ; compare DL with [DI] JNG @SKIP_EXCHANGE ; jump to label @SKIP_EXCHANGE if DL<[DI] XCHG DL, [DI] ; set DL=[DI], [DI]=DL MOV [SI], DL ; set [SI]=DL @SKIP_EXCHANGE: ; jump label INC SI ; set SI=SI+1 INC DI ; set DI=DI+1 DEC BX ; set BX=BX-1 JNZ @INNER_LOOP ; jump to label @INNER_LOOP if BX!=0 LOOP @OUTER_LOOP

Slide 9

Slide 9 text

MOV AX, SI ; set AX=SI MOV CX, BX ; set CX=BX DEC CX ; set CX=CX-1 @OUTER_LOOP: ; loop label MOV BX, CX ; set BX=CX MOV SI, AX ; set SI=AX MOV DI, AX ; set DI=AX INC DI ; set DI=DI+1 @INNER_LOOP: ; loop label MOV DL, [SI] ; set DL=[SI] CMP DL, [DI] ; compare DL with [DI] JNG @SKIP_EXCHANGE ; jump to label @SKIP_EXCHANGE if DL<[DI] XCHG DL, [DI] ; set DL=[DI], [DI]=DL MOV [SI], DL ; set [SI]=DL @SKIP_EXCHANGE: ; jump label INC SI ; set SI=SI+1 INC DI ; set DI=DI+1 DEC BX ; set BX=BX-1 JNZ @INNER_LOOP ; jump to label @INNER_LOOP if BX!=0 LOOP @OUTER_LOOP SORTING ALGORITHM

Slide 10

Slide 10 text

– Structure and Interpretation of Computer Programs “Programs must be written for people to read, and only incidentally for machines to execute.”

Slide 11

Slide 11 text

– Refactoring “Good programmers write code that humans can understand”

Slide 12

Slide 12 text

for _ in 0..

Slide 13

Slide 13 text

Concise Code

Slide 14

Slide 14 text

[self beginTaskWithName:@"MyTask" completion:^{ NSLog(@"The task is complete"); }];

Slide 15

Slide 15 text

self.beginTaskWithName("MyTask", completion: { print("The task is complete") })

Slide 16

Slide 16 text

beginTaskWithName("MyTask", completion: { print("The task is complete") })

Slide 17

Slide 17 text

beginTaskWithName("MyTask") { print("The task is complete") }

Slide 18

Slide 18 text

beginTaskWithName("MyTask") { print("The task is complete") } [self beginTaskWithName:@"MyTask" completion:^{ NSLog(@"The task is complete"); }];

Slide 19

Slide 19 text

Concise Code

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

•Value types •Protocols •Safety

Slide 23

Slide 23 text

Value Types: Immutability

Slide 24

Slide 24 text

NSArray NSData NSDictionary NSIndexSet NSSet NSString

Slide 25

Slide 25 text

NSArray NSData NSDictionary NSIndexSet NSSet NSString NSMutableArray NSMutableData NSMutableDictionary NSMutableIndexSet NSMutableSet NSMutableString

Slide 26

Slide 26 text

let var

Slide 27

Slide 27 text

state! function inputs outputs

Slide 28

Slide 28 text

Functional Programming

Slide 29

Slide 29 text

Concurrency

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

Value Types: Immutability

Slide 32

Slide 32 text

Value Types: Enumerations

Slide 33

Slide 33 text

0 30 60 90 Structs Classes Enums

Slide 34

Slide 34 text

typedef NS_ENUM(NSInteger, TransportMode) { TransportModeAirplane, TransportModeBoat, TransportModeTruck };

Slide 35

Slide 35 text

enum TransportMode { case Airplane case Boat case Truck }

Slide 36

Slide 36 text

enum TransportMode { case Airplane case Boat case Truck } func emojiForTransportMode(mode: TransportMode) -> String { switch mode { case .Airplane: return "✈" case .Boat: return "⛵" case .Truck: return "" } }

Slide 37

Slide 37 text

enum TransportMode { case Airplane case Boat case Truck func emojiRepresentation() -> String { switch self { case .Airplane: return "✈" case .Boat: return "⛵" case .Truck: return "" } } }

Slide 38

Slide 38 text

enum TransportMode { case Airplane case Boat case Truck var emojiRepresentation: String { switch self { case .Airplane: return "✈" case .Boat: return "⛵" case .Truck: return "" } } }

Slide 39

Slide 39 text

enum TransportMode: String { case Airplane = "✈" case Boat = "⛵" case Truck = "" } let t = TransportMode.Airplane t.rawValue // "✈"

Slide 40

Slide 40 text

enum TransportMode: String { case Airplane = "✈" case Boat = "⛵" case Truck = ""
 case Rocket = "✈" }

Slide 41

Slide 41 text

• Well-defined set of values • Segues • Asset catalog items Enumeration Uses

Slide 42

Slide 42 text

• Computed properties • Methods • Raw values • Associated values Enumerations

Slide 43

Slide 43 text

Protocols

Slide 44

Slide 44 text

struct Sentence { let sentence: String init(_ sentence: String) { self.sentence = sentence } // ... } let mySentence = Sentence("Hello there")

Slide 45

Slide 45 text

Value Type Behavior mySentence == otherSentence mySentence >= otherSentence print("\(mySentence)")

Slide 46

Slide 46 text

struct Sentence { let sentence: String init(_ sentence: String) { self.sentence = sentence } // ... }

Slide 47

Slide 47 text

extension Sentence: Equatable { } func ==(lhs: Sentence, rhs: Sentence) -> Bool { return lhs.sentence == rhs.sentence } mySentence == otherSentence

Slide 48

Slide 48 text

extension Sentence: Comparable { } func <(lhs: Sentence, rhs: Sentence) -> Bool { return lhs.sentence < rhs.sentence } mySentence >= otherSentence

Slide 49

Slide 49 text

print("\(mySentence)") Sentence(sentence: "Hello there") ↪

Slide 50

Slide 50 text

extension Sentence: CustomStringConvertible { var description: String { return sentence } } print("\(mySentence)") Hello there ↪

Slide 51

Slide 51 text

More Value Type Behavior dictionary[mySentence] = "value" let sentence = Sentence("Hello there") let sentence: Sentence = "Hello there" for word in mySentence { ... }

Slide 52

Slide 52 text

extension Sentence: StringLiteralConvertible { // more code here!
 init(stringLiteral value: StringLiteralType) { self.sentence = value } } let otherSentence: Sentence = "Hello there"

Slide 53

Slide 53 text

extension Sentence: Hashable { var hashValue: Int { return sentence.hashValue } } dictionary[mySentence] = "value" set.insert(mySentence)

Slide 54

Slide 54 text

extension Sentence: SequenceType { func generate() -> SentenceGenerator {
 let words =
 sentence.componentsSeparatedByString(" ") return SentenceGenerator(words: words) } }

Slide 55

Slide 55 text

struct SentenceGenerator: GeneratorType { let words: [String]
 var index = 0
 } mutating func next() -> String? { if index < words.count { let thisIndex = index index += 1 return words[thisIndex] } else { return nil } }

Slide 56

Slide 56 text

for word in mySentence { print("Word: \(word)") } Word: Hello Word: world

Slide 57

Slide 57 text

Value Type Behavior • Equality • Comparison • Printing • Hashing • String conversion • Iteration == != > < <= >= print Set/dict “…” for x in …

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

Undefined Behavior

Slide 60

Slide 60 text

Optionals?!

Slide 61

Slide 61 text

!

Slide 62

Slide 62 text

? ?? if-let guard

Slide 63

Slide 63 text

func decode() { if let x = input.next() { if _fastPath((x >> 11) != 0b1101_1) { return .Result(UnicodeScalar(x)) } else { return .Error } } return .EmptyInput } ✅ ❌

Slide 64

Slide 64 text

guard let x = input.next() else
 { return .EmptyInput } if _fastPath((x >> 11) != 0b1101_1) { return .Result(UnicodeScalar(x)) } else { return .Error } ❌

Slide 65

Slide 65 text

Preconditions & Assertions

Slide 66

Slide 66 text

Assertions “internal sanity checks that are active during testing but do not impact performance of shipping code”

Slide 67

Slide 67 text

Preconditions “Check a necessary condition for making forward progress.”

Slide 68

Slide 68 text

Preconditions precondition(index >= 0,
 "vector index out of range") precondition(ablMemory != nil, "failed to allocate memory")

Slide 69

Slide 69 text

fatalError() “Unconditionally stop execution.”

Slide 70

Slide 70 text

Debug Release assert ✔ precondition ✔ ✔ fatalError() ✔ ✔

Slide 71

Slide 71 text

•Value types •Protocols •Safety

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

No content