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. ܕ <UZQF> @yashigani Cocoaษڧձؔ੢ #60

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

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

  4. ܕͬͯԿʁ

  5. ܕ͸ৼΔ෣͍

  6. ৼΔ෣͍ • ৼΔ෣͍ = σʔλߏ଄ + ॲཧ • Ͳ͏͍͏σʔλ͕Ͳ͏ಈ࡞͢Δ͔༧૝Ͱ͖Δ •

    ΠϯλʔϑΣʔε͕ݫ֨
  7. $

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

  9. 0CKFDUJWF$

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

  11. 4XJGU

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

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

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

  15. /4%JDUJPOBSZ

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

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

    class]]) { //!!!!! name = user[@"name"]; } else { //!!!!! }
  18. https://flic.kr/p/7BD2UE Too bad ߏ଄͕ෆ໌ͳͷͰσʔλͷଘࡏΛஅఆͰ͖ͳ͍ Կ͕ೖ͍ͬͯΔͷ͔ෆ໌ͳͷͰσʔλܕΛࢦఆ ৼΔ෣͍͕ແ͍

  19. ܕ͕ແ͍

  20. .PEFM0CKFDU JO0CKFDUJWF$

  21. @interface User : NSObject @property (copy) NSString *name; @property NSUInteger

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

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

    if (user.name != nil) { cell.textLabel.text = user.name; } else { //!!!!! }
  24. https://flic.kr/p/i74K6M Too bad σʔλͷߏ଄ͱܕ͸දݱͰ͖ͨ ΠϯλʔϑΣʔε͕ෆఆ(nilͷՄೳੑ) ৼΔ෣͍͚͕ͩ͋Δ(ଋറ͕ແ͍)

  25. ܗແ͠

  26. .PEFM0CKFDU JO4XJGU

  27. struct User { let name: String let age: Int let

    isPremium: Bool }
  28. let user = //... let cell = tableView.dequeueReusableCellWithId entifier("cell") cell?.titleLabel.text

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

  30. 5IJTJTܕ

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

  32. 0OFNPSFUIJOH

  33. 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... }
  34. https://flic.kr/p/DC3Q Too bad user΋error΋nilͷͱ͖͸Ͳ͏͍͏Τϥʔʁ ΋ͬͱ΋ͬͱstrictʹදݱ͍ͨ͠ (User?, NSError?)͸ૉ๿͗͢Δ

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

  36. 3FTVMU5

  37. enum Result<T> { case Success(T) case Failure(NSError) init(_ value: T)

    { self = .Success(value) } init(_ error: NSError) { self = .Failure(error) } }
  38. ܕഁΓٕͦͷ ʙHFOFSJDTʙ

  39. enum Result<T> { case Success(T) case Failure(NSError) init(_ value: T)

    { self = .Success(value) } init(_ error: NSError) { self = .Failure(error) } } generics
  40. ҦΘ͹ϫΠϧυΧʔυܕ • ͱΓ͋͑ͣ͸ͳΜͰ΋ೖΕΒΕΔܕ • ಉ͡ϥϕϧͷ΋ͷ͸ಉ͡ܕ • ΠϯλʔϑΣʔεΛܾΊͨͱ͖ʹܕ͕ܾ·Δ • func parse(_:AnyObject)

    -> Result<User> • Any/AnyObjectͱҧͬͯղ͘ͱ͖ʹ౎౓ͳʹ͔
 ͔֬ΊΔඞཁ͕ແ͍
  41. ܕഁΓٕͦͷ ʙFOVNʙ

  42. enum Result<T> { case Success(T) case Failure(NSError) init(_ value: T)

    { self = .Success(value) } init(_ error: NSError) { self = .Failure(error) } } enum
  43. ෳ਺ͷঢ়ଶΛऔΓ͏Δ • Aঢ়ଶͳΒaΛ࣋ͬͯΔ͚ͲɼBঢ়ଶͰ͸࣋ͬͯͳ͍ • ͱΓ͋͑ͣAnyObjectʹͿͪࠐ·ͳ͍͍ͯ͘ • ͲΕ͔ͻͱͭͷঢ়ଶ͔͠औΓಘͳ͍ • A͔ͭBͳঢ়ଶ͸ଘࡏ͠ͳ͍

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

  45. func parse(JSON: AnyObject?) -> Result<User> let JSON: AnyObject? = //

    ... let result = parse(JSON) switch result { case .Success(let user): // ... case .Failure(let error): // ... }
  46. +VTUB&JUIFS

  47. 0QUJPOBM

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

    • NoneͩͬͨΒnilΛ࣋ͬͯΔ
  49. 5IJTJTܕഁΓ

  50. ·ͱΊ

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

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