Swiftのコードをより読みやすく、理解しやすくするための実践的なTips

 Swiftのコードをより読みやすく、理解しやすくするための実践的なTips

2016/08/30 iOSDC Reject Conference day1

Ac3c0c2b458cdf0fe6a6e98a46e34322?s=128

Yoshikuni Kato

August 30, 2016
Tweet

Transcript

  1. 1

  2. Tips 1. guard ͰૣظϦλʔϯ͢Δ 2. Result Ͱ੒ޭ/ࣦഊΛද͢ 3. ʢ͓·͚ʣFRP Λ࢖͏ͱΠϕϯτॲཧ͕ಡΈ΍͘͢ͳΔ

    2
  3. 1. guard ͰૣظϦλʔϯ͢Δ 3

  4. guard ͰૣظϦλʔϯ͢Δ if ৚݅ { ϝΠϯॲཧ } else { Τϥʔॲཧ

    } --- guard ৚݅ else { Τϥʔॲཧ return // <- returnΛ๨Εͳ͘ͳΔʂ } ϝΠϯॲཧ // <- ωετ͕ઙ͘ͳΔʂ 4
  5. swi$3ͷ guard [SE-0099] ACCEPTED: Restructuring Condi;on Clauses // swift2 guard

    let x = optional where y == 2 else { // swift3 guard let x = optional, y == 2 else { 5
  6. 2. Result Ͱ੒ޭ/ࣦഊΛද͢ 6

  7. Result ͱ͸ ੒ޭ / ࣦഊΛද͢enum enum Result<T, E> { case

    Success(T) case Failure(E) } 7
  8. Result Ͱ੒ޭ/ࣦഊΛද͢ // Resultͳ͠ʢObjective-Cతͳॻ͖ํʣ fetchData(callback: { (data: AnyObject?, error: NSError?)

    in // data == nil && error == nil ͷ৔߹Ͳ͏͠Α͏! // data != nil && error != nil ͷ৔߹Ͳ͏͠Α͏! }) // ResultΛ࢖͏ fetchData(callback: { (result: Result<AnyObject, NSError>) in switch result { case .Success(let value): // value͸nilͰ͸ͳ͍" case .Failure(let error): // error͸nilͰ͸ͳ͍" } }) 8
  9. Result ิ଍ • OSS: an(typical/Result • σϑΝΫτελϯμʔυʢReac(veCocoa / APIKitͳͲར༻ʣ •

    throw (swi=2~)ͱͷؔ܎ • Swi= 2 Error Handling vs Result<T, E> by Yasuhiro Inami 9
  10. ͦͷଞͷTips • nested function ͰॲཧΛ·ͱΊΔ • op%onalΛద੾ʹઃఆ͢Δ ʢεϥΠυิ଍Λࢀর͍ͯͩ͘͠͞ʣ 10

  11. 3. ʢ͓·͚ʣFRP Λ࢖͏ͱ Πϕϯτॲཧ͕ಡΈ΍͘͢ͳΔ 11

  12. Func$onal Reac$ve Programming ͷಛ௃ • ϦΞΫςΟϒͰ͋ΔʢΠϕϯτΛݕ஌ͯ͠దԠ͢Δʣ • એݴతʹॻ͚Δʢ㲈ؔ਺ܕతʣ -> ཧղ͠΍͍͢ίʔυʹͳΔ

    ʢίʔυΛಡΊ͹ɺॲཧ಺༰Λ೺ѲͰ͖Δʣ 12
  13. ྫ • ʢετϦʔϜͷϚʔδͷྫʣ • Ξϥʔτදࣔ͢Δ • ΩϟϯηϧϘλϯΛλοϓͯ͠ด͡Δ • എܠλοϓͯ͠΋ด͡Δ 13

  14. 14

  15. 15

  16. 16

  17. 17

  18. FRP Λ࢖͏ͱΠϕϯτॲཧ͕ಡΈ΍͘͢ͳΔ • ϦΞΫςΟϒͰ͋ΓɺએݴతͰ͋Δ • ӡ༻͢Δ্Ͱ΋ɺࠩ෼ɾมߋ͕෼͔Γ΍͍͢ • ʮίʔυͷཧղ͠΍͢͞ʯͱ͍͏؍఺͔Β΋ಋೖݕ౼ͷՁ஋͋ Γ 18

  19. Tips 1. guard ͰૣظϦλʔϯ͢Δ 2. Result Ͱ੒ޭ/ࣦഊΛද͢ 3. ʢ͓·͚ʣFRP Λ࢖͏ͱΠϕϯτॲཧ͕ಡΈ΍͘͢ͳΔ

    19
  20. Swi$ΤϯδχΞืूத! • ͖Ε͍ͳUIͷΞϓϦ͕࡞Γ͍ͨʂ • ӡ༻ΑΓ৽ن։ൃ͕͍ͨ͠ʂ • ٕज़తʹ΋ઃܭϑΣʔζ͔Βͩ͜ΘΓ ͍ͨʂ ࡉ෦·Ͱͩ͜ΘͬͨσβΠϯΛ࣮૷ͨ͠ ͍iOSΤϯδχΞืूʂ

    20
  21. Thank you! 21

  22. ࢀߟ • Ϧʔμϒϧίʔυ――ΑΓྑ͍ίʔυΛॻͨ͘ΊͷγϯϓϧͰ࣮ફతͳ ςΫχοΫ • Swi& 2 Error Handling vs

    Result<T, E> by Yasuhiro Inami • Safer Networking Layer With Swi& by Yusei Nishiyama • ReacHveCocoa • APIKit • this week in Swi& 22
  23. ิ଍ 23

  24. nested function ͰॲཧΛ·ͱΊΔ 24

  25. nested function ͰॲཧΛ·ͱΊΔ • Α͋͘Δྫ func viewDidLoad() { super.viewDidLoad() configureSomeView()

    } private func configureSomeView() { } • nested function Ͱ·ͱΊΔͱɾɾɾ • ʮಛఆͷؔ਺ͷதͰ࢖͏ʯ͜ͱ͕໌֬ʹͳΔ • ଞͷॲཧ಺Ͱ࢖ΘΕΔڪΕ͕ͳ͍ func viewDidLoad() { func configureSomeView() { } super.viewDidLoad() configureSomeView() } 25
  26. op#onalΛద੾ʹઃఆ͢Δ 26

  27. op#onalͷҙຯ • non-op&onal • ઈରʹ஋͕͋Δ • ? (op&onal) • nilʹͳΔՄೳੑ͕͋Δ

    • ! (Implicitly Unwrapped Op&onal) • nil͸ೖΒͳ͍͕ʢswi;΍frameworkͷ࢓૊Έ্ʣҰ୴ɺnilͰॳظ Խͤ͟ΔΛಘͳ͍ 27
  28. IUOΛ࢖͏΂͖ঢ়گ • IBOutlet • VCʹViewModelɺDataSourceͳͲΛinject͍ͨ͠ && storyboard࢖༻ • storyboardΛ࢖͏ͱɺVCͷಠࣗini9alizerΛ࢖͑ͳ͍ •

    ແཧʹop9onal(?)ʹ͢ΔΑΓɺIUOʹͯ͠ઃܭͷҙਤΛ໌Β͔ ʹ͢Δ 28
  29. ʢࢀߟʣstoryboard vs dependency injec1on • Init based Storyboard View Controller

    Instan4a4on by Javi Soto • ObjCͷίʔυΛ࢖͏ͱɺstoryboardར༻࣌Ͱ΋ಠࣗini4alizerΛ࢖͑Δɻʢ͔͠ ͠ɺϓϩύςΟ͸IUOͷ··ʣ • Building Fabric.app in SwiB by Javi Soto • View Controller Data Injec4on with Storyboards and Segues in SwiB by Natasha The Robot • ίϝϯτͰٞ࿦͕ߦΘΕ͍ͯΔ • protocolͰಡΈ΍͘͢͢Δํ๏͕঺հ͞Ε͍ͯΔ 29
  30. ʢࢀߟʣswi$3ͷIUO • IUOΛผͷม਺ʹ୅ೖͨ࣌͠ͳͲͷڍಈʢܕʣ͕มΘΔ • WWDC 2016: Increased Safety in Swi;

    3.0 by Big Nerd Ranch 30