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

Types in Swift

yashigani
February 28, 2015

Types in Swift

Cocoa勉強会関西 #60の発表資料です

yashigani

February 28, 2015
Tweet

More Decks by yashigani

Other Decks in Programming

Transcript

  1. $

  2. NSDictionary *user = //... NSString *name; if ([user[@"name"] isKindOfClass: [NSString

    class]]) { //!!!!! name = user[@"name"]; } else { //!!!!! }
  3. @interface User : NSObject @property (copy) NSString *name; @property NSUInteger

    age; @property (getter=isPremium) BOOL premium; @end
  4. User *user = //... UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@" cell"];

    if (user.name != nil) { cell.textLabel.text = user.name; } else { //!!!!! }
  5. func parse(JSON: AnyObject?) -> (User?, NSError?) let JSON: AnyObject? =

    //... let (user, error) = parse(JSON) if let e = error { //... return } if let u = user { //... } else { // maybe user is nil... }
  6. enum Result<T> { case Success(T) case Failure(NSError) init(_ value: T)

    { self = .Success(value) } init(_ error: NSError) { self = .Failure(error) } }
  7. enum Result<T> { case Success(T) case Failure(NSError) init(_ value: T)

    { self = .Success(value) } init(_ error: NSError) { self = .Failure(error) } } generics
  8. enum Result<T> { case Success(T) case Failure(NSError) init(_ value: T)

    { self = .Success(value) } init(_ error: NSError) { self = .Failure(error) } } enum
  9. func parse(JSON: AnyObject?) -> Result<User> let JSON: AnyObject? = //

    ... let result = parse(JSON) switch result { case .Success(let user): // ... case .Failure(let error): // ... }