Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Practical Swift

Practical Swift

Presented at NSCoder Night Copenhagen July 23, 2014 @Robocat

Kristian Andersen

July 23, 2014
Tweet

More Decks by Kristian Andersen

Other Decks in Programming

Transcript

  1. New toys • Generics • Improved Enums • Optionals •

    Extensions • Nested types • Pattern matching
  2. enum ShowStatus { case Ended case Continuing init(string: String) {

    switch (string) { case "ended": fallthrough case "cancelled": self = .Ended case "continuing": fallthrough default: self = .Continuing } } }
  3. let handler: (RequestResult<JSONValue>) -> 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)
  4. - (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; }
  5. 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) } } } }
  6. 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) } } } }
  7. 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) } } } }
  8. 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) } } } }
  9. 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() }
  10. 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() }
  11. 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 }
  12. class ShowDetailViewController: UIViewController { var detailView: ShowDetailView { return view

    as ShowDetailView } func fetchShowUpdates() { detailView.startLoadingIndicator() ... } ... }
  13. 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