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. ܕ

    @yashigani
    Cocoaษڧձؔ੢ #60

    View full-size slide

  2. yashigani
    id:yashigani_w @yashigani
    Mobile Application Engineer
    http://yashigani.hatenablog.com
    Hatena

    View full-size slide

  3. https://flic.kr/p/nBzE6V
    ܕʹϋϚͬͨ͜ͱ͸͋Γ·͔͢
    • ίϯύΠϧΤϥʔʹͳΔ΍ͭͰ͠ΐʁ
    • ܕ੍ͬͯ໿ͳ͚ͩͰͱʹ͔͘अຐ
    • id࠷ߴ΍Μʂʂʂܕͱ͔͍ΒΜʂʂʂ

    View full-size slide

  4. ܕ͸ৼΔ෣͍

    View full-size slide

  5. ৼΔ෣͍
    • ৼΔ෣͍ = σʔλߏ଄ + ॲཧ
    • Ͳ͏͍͏σʔλ͕Ͳ͏ಈ࡞͢Δ͔༧૝Ͱ͖Δ
    • ΠϯλʔϑΣʔε͕ݫ֨

    View full-size slide

  6. ΞυϨε௕ͷϝλσʔλ
    • ϝϞϦۭؒʹ͓͚Δม਺ͷҐஔ
    • ௿ϨΠϠͷॲཧ͕Մೳͳނ
    • ϓϦϛςΟϒ͗͢Δ

    View full-size slide

  7. ৼΔ෣͍ͷϝλσʔλ
    • isaϙΠϯλ
    • ม਺͕ৼΔ෣͏΂͖͜ͱΛࣔࠦ(ಈతσΟεύον)
    • ͨͩ͠ඞͣ͠΋ै͑Δͱ͸ݶΒͳ͍
    • ม਺ͷৼΔ෣͍Λଋറ͢Δ΋ͷͰ͸ແ͍

    View full-size slide

  8. ৼΔ෣͍Λ໿ଋ͢Δ΋ͷ
    • ৼΔ෣͍Λม਺ʹଋറ
    • ͋Δܕͷม਺͸ͦͷܕͷৼΔ෣͍͔͠͠ͳ͍
    • ܕͷ͋Δϓϩάϥϛϯά͕Մೳ

    View full-size slide

  9. ͳͥϓϩάϥϜʹ
    ܕ͕ඞཁͳͷ͔

    View full-size slide

  10. ܕ
    ܕ͕͋Δ͔ΒɺܕഁΓɻ
    ܕ͕ͳ͚Ε͹୯ͳΔʮܗແ͠ʯ
    ʕेീ୅໨தଜצࡾ࿠ Վ෣ب໾ऀ

    View full-size slide

  11. /4%JDUJPOBSZ

    View full-size slide

  12. NSDictionary *user = //...
    NSString *name = user[@"name"];

    View full-size slide

  13. NSDictionary *user = //...
    NSString *name;
    if ([user[@"name"] isKindOfClass:
    [NSString class]]) { //!!!!!
    name = user[@"name"];
    }
    else {
    //!!!!!
    }

    View full-size slide

  14. https://flic.kr/p/7BD2UE
    Too bad
    ߏ଄͕ෆ໌ͳͷͰσʔλͷଘࡏΛஅఆͰ͖ͳ͍
    Կ͕ೖ͍ͬͯΔͷ͔ෆ໌ͳͷͰσʔλܕΛࢦఆ
    ৼΔ෣͍͕ແ͍

    View full-size slide

  15. .PEFM0CKFDU
    JO0CKFDUJWF$

    View full-size slide

  16. @interface User : NSObject
    @property (copy) NSString *name;
    @property NSUInteger age;
    @property (getter=isPremium) BOOL
    premium;
    @end

    View full-size slide

  17. User *user = //...
    UITableViewCell *cell =
    [tableView
    dequeueReusableCellWithIdentifier:@"
    cell"];
    cell.textLabel.text = user.name;

    View full-size slide

  18. User *user = //...
    UITableViewCell *cell =
    [tableView
    dequeueReusableCellWithIdentifier:@"
    cell"];
    if (user.name != nil) {
    cell.textLabel.text = user.name;
    }
    else {
    //!!!!!
    }

    View full-size slide

  19. https://flic.kr/p/i74K6M
    Too bad
    σʔλͷߏ଄ͱܕ͸දݱͰ͖ͨ
    ΠϯλʔϑΣʔε͕ෆఆ(nilͷՄೳੑ)
    ৼΔ෣͍͚͕ͩ͋Δ(ଋറ͕ແ͍)

    View full-size slide

  20. .PEFM0CKFDU
    JO4XJGU

    View full-size slide

  21. struct User {
    let name: String
    let age: Int
    let isPremium: Bool
    }

    View full-size slide

  22. let user = //...
    let cell =
    tableView.dequeueReusableCellWithId
    entifier("cell")
    cell?.titleLabel.text = user.name

    View full-size slide

  23. https://flic.kr/p/egaNwP
    Relaxed
    ෆ֬ఆͳཁૉ͸ϏδωεϩδοΫʹӅṭ
    ΞϓϦέʔγϣϯ͸σʔλΛදࣔ͢Δ͚ͩ
    ৼΔ෣͍Λଋറ͢Δ͜ͱͰ໿ଋ͞Εͨڍಈ

    View full-size slide

  24. ܕͷ͋ΔϓϩάϥϜ
    • ৼΔ෣͍͕໌֬Ͱ׬શ
    • ͕ͨͬͯ͠ෆඞཁͳ৺഑͕͍Βͳ͍
    • ϏδωεϩδοΫͱΞϓϦέʔγϣϯͷ෼཭
    • ΠϯλʔϑΣʔεͷΈʹґଘ࣮ͨ͠૷

    View full-size slide

  25. 0OFNPSFUIJOH

    View full-size slide

  26. 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...
    }

    View full-size slide

  27. https://flic.kr/p/DC3Q
    Too bad
    user΋error΋nilͷͱ͖͸Ͳ͏͍͏Τϥʔʁ
    ΋ͬͱ΋ͬͱstrictʹදݱ͍ͨ͠
    (User?, NSError?)͸ૉ๿͗͢Δ

    View full-size slide

  28. ܕΛഁΔ
    ܕʹΑΔܕͷந৅Խ

    View full-size slide

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

    View full-size slide

  30. ܕഁΓٕͦͷ
    ʙHFOFSJDTʙ

    View full-size slide

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

    View full-size slide

  32. ҦΘ͹ϫΠϧυΧʔυܕ
    • ͱΓ͋͑ͣ͸ͳΜͰ΋ೖΕΒΕΔܕ
    • ಉ͡ϥϕϧͷ΋ͷ͸ಉ͡ܕ
    • ΠϯλʔϑΣʔεΛܾΊͨͱ͖ʹܕ͕ܾ·Δ
    • func parse(_:AnyObject) -> Result
    • Any/AnyObjectͱҧͬͯղ͘ͱ͖ʹ౎౓ͳʹ͔

    ͔֬ΊΔඞཁ͕ແ͍

    View full-size slide

  33. ܕഁΓٕͦͷ
    ʙFOVNʙ

    View full-size slide

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

    View full-size slide

  35. ෳ਺ͷঢ়ଶΛऔΓ͏Δ
    • Aঢ়ଶͳΒaΛ࣋ͬͯΔ͚ͲɼBঢ়ଶͰ͸࣋ͬͯͳ͍
    • ͱΓ͋͑ͣAnyObjectʹͿͪࠐ·ͳ͍͍ͯ͘
    • ͲΕ͔ͻͱͭͷঢ়ଶ͔͠औΓಘͳ͍
    • A͔ͭBͳঢ়ଶ͸ଘࡏ͠ͳ͍

    View full-size slide

  36. ͭ·ΓResultͱ͸
    • ੒ޭ͔ࣦഊ͔ΛऔΓ͏Δ஋
    • ੒ޭͨ͠ΒT͕ɼࣦഊͨ͠ΒNSError͕ಘΒΕΔ
    • ୯७ʹOptional஋Λฒ΂Δͷʹൺ΂ͯѻ͍΍͍͢
    • ྆ํ஋͕͋Δ྆ํແ͍ͳΜͯ͜ͱ͸͋Γ͑ͳ͍

    View full-size slide

  37. func parse(JSON: AnyObject?) ->
    Result
    let JSON: AnyObject? = // ...
    let result = parse(JSON)
    switch result {
    case .Success(let user): // ...
    case .Failure(let error): // ...
    }

    View full-size slide

  38. Optional΋ಉྨ
    • Optional͸ͻͱͭͷgenericܕΛऔΔenum
    • enum Optional
    • ͳʹ͔ͷ஋͕͋Δ/ͳ͍ͷ2छྨͷঢ়ଶΛऔΓ͏Δ
    • SomeͩͬͨΒTΛ࣋ͬͯΔ
    • NoneͩͬͨΒnilΛ࣋ͬͯΔ

    View full-size slide

  39. 5IJTJTܕഁΓ

    View full-size slide

  40. ܕ͸ৼΔ෣͍ͱ஌Δ
    ܕͷ͋ΔϓϩάϥϜ
    ܕΛഁΔ

    View full-size slide

  41. ͋Γ͕ͱ͏͍͟͝·ͨ͠

    View full-size slide