Slide 1

Slide 1 text

A SWIFT APPROACH KYLEFULLER

Slide 2

Slide 2 text

KYLEFULLER

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

NEW CONCEPTS

Slide 6

Slide 6 text

RUNTIME

Slide 7

Slide 7 text

@objc / NSObject

Slide 8

Slide 8 text

NSKeyValueObserving

Slide 9

Slide 9 text

RUNTIME

Slide 10

Slide 10 text

WWDC 2015

Slide 11

Slide 11 text

OPTIONALS?

Slide 12

Slide 12 text

func compute() -> Bool?

Slide 13

Slide 13 text

true false neither

Slide 14

Slide 14 text

LACK OF A VALUE

Slide 15

Slide 15 text

NOT AN EMPTY VALUE

Slide 16

Slide 16 text

CAN'T WE DO THIS IN OBJECTIVE-C?

Slide 17

Slide 17 text

nil Nil NULL CGRectNull -1 0 NSNotFound NSNull ...

Slide 18

Slide 18 text

EXPRESS YOU DON'T ACCEPT NIL

Slide 19

Slide 19 text

EXPRESS YOU WILL NOT RETURN NIL

Slide 20

Slide 20 text

/// You **MUST** pass in a URL - (instancetype)initWithURL:(NSURL *)URL { NSParameterAssert(URL != nil); }

Slide 21

Slide 21 text

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)path { return nil; }

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

RUNTIME ERROR

Slide 24

Slide 24 text

RUNTIME ERROR

Slide 25

Slide 25 text

COMPILE ERROR

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

TUPLES

Slide 28

Slide 28 text

func get(username:String) -> (Person?, NSError?)

Slide 29

Slide 29 text

let person, error = get("Kyle")

Slide 30

Slide 30 text

OBJECTIVE-C

Slide 31

Slide 31 text

NSError *error; Person *person = [Person getPerson:@"Kyle" error:&error];

Slide 32

Slide 32 text

NAMED TUPLES

Slide 33

Slide 33 text

func get(username:String) -> (person:Person?, error:NSError?)

Slide 34

Slide 34 text

let result = get("Kyle") result.person result.error

Slide 35

Slide 35 text

- (NSArray *)getPerson:(NSString *)username; - (NSDictionary *)getPerson:(NSString *)username;

Slide 36

Slide 36 text

NSDictionary *components = [Person getPerson:@"kyle"]; Person *person = components["person"]; NSError *error = components["error"];

Slide 37

Slide 37 text

TYPE SAFETY?

Slide 38

Slide 38 text

FRAGILE

Slide 39

Slide 39 text

ENUMERATIONS

Slide 40

Slide 40 text

enum Result { case Success(String) case Error(Error) }

Slide 41

Slide 41 text

switch result { case .Error(let error): println("There was an error (\(error)).") case .Success(let string): println("\(string)") }

Slide 42

Slide 42 text

CLOSURES

Slide 43

Slide 43 text

EVERYTHING IS A CLOSURE

Slide 44

Slide 44 text

class TestObject { func testA() -> () { println("first test") } var testB:(() -> ()) = { println("second test") } }

Slide 45

Slide 45 text

let testing = TestObject() testing.testA() testing.testB()

Slide 46

Slide 46 text

testing.testB = testing.testA

Slide 47

Slide 47 text

testing.testB() // Actually calls testA

Slide 48

Slide 48 text

CONSISTENCY

Slide 49

Slide 49 text

FUNCTIONAL

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

let string = "Hello World" let matches:[NSTextCheckingResult] = /* regex for words in string */ matches.map { string.substringWithRange($0.range) }

Slide 52

Slide 52 text

let string = "Hello World" let matches:[NSTextCheckingResult] = /* regex for words in string */ var strings = [String]() for match in matches { let catch = string.substringWithRange(match.range) strings.append(catch) }

Slide 53

Slide 53 text

func reduce(initial, combine) -> result

Slide 54

Slide 54 text

combine : ((previous, current) -> (result))

Slide 55

Slide 55 text

let items = [["a", "b"], ["c", "d"], ["e"]] items.reduce([]) { initial, expressions -> [String] in initial + expressions } => ["a", "b", "c", "d", "e"]

Slide 56

Slide 56 text

let items = [["a", "b"], ["c", "d"], ["e"]] items.reduce([], +) => ["a", "b", "c", "d", "e"]

Slide 57

Slide 57 text

func +(lhs:[T], rhs:[T]) -> [T]

Slide 58

Slide 58 text

items.sort { $0 > $1 }

Slide 59

Slide 59 text

items.sort(>)

Slide 60

Slide 60 text

items.filter { $0.hasPermission }

Slide 61

Slide 61 text

func isAdmin(user:User) -> Bool { return user.username == "Kyle" } users.filter(isAdmin)

Slide 62

Slide 62 text

GENERICS

Slide 63

Slide 63 text

Array

Slide 64

Slide 64 text

Dictionary

Slide 65

Slide 65 text

func max(a:T, b:T) -> T { return a > b ? a : b }

Slide 66

Slide 66 text

max(anInteger, anotherInteger) max(aFloat, anotherFloat) max(aString, anotherString)

Slide 67

Slide 67 text

PATTERN MATCHING

Slide 68

Slide 68 text

let point = (1, 2) switch point { case (0, 0): println("(0, 0) is at the origin.") case (-2...2, -2...2): println("(\(point.0), \(point.1)) is near the origin.") default: println("The point is at (\(point.0), \(point.1)).") } // prints "(1, 2) is near the origin."

Slide 69

Slide 69 text

@auto_closure

Slide 70

Slide 70 text

delay(5, println("Hello World"))

Slide 71

Slide 71 text

func delay(time:UInt32, block:@auto_closure) { sleep(time) block() }

Slide 72

Slide 72 text

BUILDING ASSERT() IN SWIFT

Slide 73

Slide 73 text

assert(someExpensiveComputation() != 42)

Slide 74

Slide 74 text

func assert(predicate : () -> Bool) { #if !NDEBUG if !predicate() { abort() } #endif }

Slide 75

Slide 75 text

PLAYGROUNDS

Slide 76

Slide 76 text

PLAYGROUNDS

Slide 77

Slide 77 text

SWIFT

Slide 78

Slide 78 text

SAFER

Slide 79

Slide 79 text

CONVENTIONS

Slide 80

Slide 80 text

CONVENTIONS

Slide 81

Slide 81 text

LANGUAGE CONSTRAINTS

Slide 82

Slide 82 text

BETTER CODE

Slide 83

Slide 83 text

BETTER DEVELOPERS

Slide 84

Slide 84 text

LESS BUGS

Slide 85

Slide 85 text

WE'RE ALL BEGINNERS

Slide 86

Slide 86 text

PERFECTING

Slide 87

Slide 87 text

BREAKING CHANGES

Slide 88

Slide 88 text

- Xcode 6.1 + Xcode 6.1.1

Slide 89

Slide 89 text

FOR GOOD, NOT EVIL

Slide 90

Slide 90 text

!

Slide 91

Slide 91 text

FILE RADARS

Slide 92

Slide 92 text

FUTURE

Slide 93

Slide 93 text

COMMON DESIGN PATTERNS

Slide 94

Slide 94 text

KYLEFULLER