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

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

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

2016/08/30 iOSDC Reject Conference day1

Yoshikuni Kato

August 30, 2016
Tweet

More Decks by Yoshikuni Kato

Other Decks in Programming

Transcript

  1. 1

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

    } --- guard ৚݅ else { Τϥʔॲཧ return // <- returnΛ๨Εͳ͘ͳΔʂ } ϝΠϯॲཧ // <- ωετ͕ઙ͘ͳΔʂ 4
  3. 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
  4. 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
  5. Result ิ଍ • OSS: an(typical/Result • σϑΝΫτελϯμʔυʢReac(veCocoa / APIKitͳͲར༻ʣ •

    throw (swi=2~)ͱͷؔ܎ • Swi= 2 Error Handling vs Result<T, E> by Yasuhiro Inami 9
  6. 14

  7. 15

  8. 16

  9. 17

  10. ࢀߟ • Ϧʔμϒϧίʔυ――ΑΓྑ͍ίʔυΛॻͨ͘ΊͷγϯϓϧͰ࣮ફతͳ ςΫχοΫ • 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
  11. nested function ͰॲཧΛ·ͱΊΔ • Α͋͘Δྫ func viewDidLoad() { super.viewDidLoad() configureSomeView()

    } private func configureSomeView() { } • nested function Ͱ·ͱΊΔͱɾɾɾ • ʮಛఆͷؔ਺ͷதͰ࢖͏ʯ͜ͱ͕໌֬ʹͳΔ • ଞͷॲཧ಺Ͱ࢖ΘΕΔڪΕ͕ͳ͍ func viewDidLoad() { func configureSomeView() { } super.viewDidLoad() configureSomeView() } 25
  12. op#onalͷҙຯ • non-op&onal • ઈରʹ஋͕͋Δ • ? (op&onal) • nilʹͳΔՄೳੑ͕͋Δ

    • ! (Implicitly Unwrapped Op&onal) • nil͸ೖΒͳ͍͕ʢswi;΍frameworkͷ࢓૊Έ্ʣҰ୴ɺnilͰॳظ Խͤ͟ΔΛಘͳ͍ 27
  13. ʢࢀߟʣ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