Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Hi! I’m Kristian @ksmandersen

Slide 3

Slide 3 text

Practical Swift

Slide 4

Slide 4 text

New toys

Slide 5

Slide 5 text

New toys

Slide 6

Slide 6 text

New toys • Generics • Improved Enums • Optionals • Extensions • Nested types • Pattern matching

Slide 7

Slide 7 text

ENUMS

Slide 8

Slide 8 text

enum { TSNotificationTimeUnitHours = 1, TSNotificationTimeUnitMinutes, TSNotificationTimeUnitDays }; typedef NSInteger TSNotificationTimeUnit;

Slide 9

Slide 9 text

enum NotificationTimeUnit { case Hours case Minutes case Days }

Slide 10

Slide 10 text

enum ShowStatus { case Ended case Continuing init(string: String) { switch (string) { case "ended": fallthrough case "cancelled": self = .Ended case "continuing": fallthrough default: self = .Continuing } } }

Slide 11

Slide 11 text

Networking

Slide 12

Slide 12 text

enum RequestResult { case Result(T) case Error(NSError) }

Slide 13

Slide 13 text

let handler: (RequestResult) -> Void = { response in switch(response) { case .Error(let error): callback(.Error(error)) case .Result(let json): var shows = [APIShow]() if let dicts = json.array { for dict in dicts { shows += APIShow(json: dict) } } callback(.Result(shows)) } } sendRequest(request, completion: handler)

Slide 14

Slide 14 text

JSON Parsing

Slide 15

Slide 15 text

- (instancetype)initWithJSON:(NSDictionary *)json { self = [super init]; if (!self) return nil; NSString *title = json[@"title"]; if (title && [title isKindOfClass:[NSString class]]) { self.title = title; } NSDictionary *stats = json[@"stats"]; if (stats && [stats isKindOfClass:[NSDictionary class]]) { NSNumber *watchers = stats[@"watchers"]; if (watchers && [watchers isKindOfClass:[NSNumber class]]) { self.watchers = [watchers integerValue]; } } self.actors = [NSMutableArray array]; NSDictionary *people = json[@"people"]; if (people && [people isKindOfClass:[NSDictionary class]]) { NSArray *actors = people[@"actors"]; if (actors && [actors isKindOfClass:[NSDictionary class]]) { for (NSDictionary *actor in actors) { [self.actors addObject:[[APIActor alloc] initWithJSON:actor]]; } } } return self; }

Slide 16

Slide 16 text

init(json: NSDictionary) { if let title = json["title"] as String? { self.title = title } if let stats = json["stats"] as? NSDictionary { if let watchers = stats["watchers"] as? NSNumber { self.watchers = watchers.integerValue } } if let people = json["people"] as? NSDictionary { if let actors = people["actors"] as? NSArray { for actor in actors { self.actors += APIActor(json: actor) } } } }

Slide 17

Slide 17 text

init(json: NSDictionary) { if let title = json["title"] as String? { self.title = title } if let stats = json["stats"] as? NSDictionary { if let watchers = stats["watchers"] as? NSNumber { self.watchers = watchers.integerValue } } if let people = json["people"] as? NSDictionary { if let actors = people["actors"] as? NSArray { for actor in actors { self.actors += APIActor(json: actor) } } } }

Slide 18

Slide 18 text

init(json: NSDictionary) { if let title = json["title"] as String? { self.title = title } if let watchers = (json["stats"] as? NSDictionary)?["watchers"] as? NSNumber { self.watchers = watchers.integerValue } if let actors = (json["people"] as? NSDictionary)?["actors"] as? NSArray { for actor in actors { if let dict = actor as? NSDictionary { self.actors += APIActor(json: dict) } } } }

Slide 19

Slide 19 text

init(json: JSONValue) { if let title = json["title"].string { self.title = title } if let watchers = json["stats"]["watchers"].number { self.watchers = Int(watchers) } if let actors = json["people"]["actors"].array { for actor in actors { if let dict = actor as? NSDictionary { self.actors += APIActor(json: dict) } } } }

Slide 20

Slide 20 text

WORKAROUNDS

Slide 21

Slide 21 text

WORKAROUNDS

Slide 22

Slide 22 text

class var entityName: String = "TVShow"

Slide 23

Slide 23 text

class var entityName: String = "TVShow"

Slide 24

Slide 24 text

class func entityName() -> String { return "TVShow" }

Slide 25

Slide 25 text

enum RequestResult { case Result(T) case Error(NSError) }

Slide 26

Slide 26 text

enum RequestResult { case Result(T) case Error(NSError) }

Slide 27

Slide 27 text

enum RequestResult { case Result(JSONValue) case Error(NSError) }

Slide 28

Slide 28 text

init(alotOfParameters...) { self.fetchRequest = fetchRequest self.cellReuseIdentifier = cellReuseIdentifier self.configureClosure = configureClosure self.collectionView = collectionView self.managedObjectContext = managedObjectContext collectionView.dataSource = self fetchedResultsController.delegate = self super.init() }

Slide 29

Slide 29 text

init(alotOfParameters...) { self.fetchRequest = fetchRequest self.cellReuseIdentifier = cellReuseIdentifier self.configureClosure = configureClosure self.collectionView = collectionView self.managedObjectContext = managedObjectContext collectionView.dataSource = self fetchedResultsController.delegate = self super.init() }

Slide 30

Slide 30 text

init(alotOfParameters...) { self.fetchRequest = fetchRequest self.cellReuseIdentifier = cellReuseIdentifier self.configureClosure = configureClosure self.collectionView = collectionView self.managedObjectContext = managedObjectContext super.init() collectionView.dataSource = self fetchedResultsController.delegate = self }

Slide 31

Slide 31 text

class ShowDetailViewController: UIViewController { var detailView: ShowDetailView { return view as ShowDetailView } func fetchShowUpdates() { detailView.startLoadingIndicator() ... } ... }

Slide 32

Slide 32 text

Awesome Projects • SwiftyJSON ~ JSON parsing
 github.com/lingoer/SwiftyJSON • QueryKit ~ Core Data querying
 github.com/kylef/QueryKit • CLIKit ~ Build command line utilities
 github.com/kylef/CLIKit • Quick ~ A BDD style testing framework
 https://github.com/Quick/Quick

Slide 33

Slide 33 text

Awesome Swift github.com/matteocrippa/awesome-swift

Slide 34

Slide 34 text

Questions?

Slide 35

Slide 35 text

Questions?