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

API Design in Swift

API Design in Swift

#WWDCPAD の発表資料です http://connpass.com/event/6877/

9278c3a06b8d8752fb913dea93f959c1?s=128

FUJI Goro

June 20, 2014
Tweet

Transcript

  1. API Design In Swift #WWDCPAD FUJI Goro (@__gfx__), a software

    engineer in COOKPAD WWDC2014 reports at COOKPAD, 2014/6/20
  2. FUJI Goro (@__gfx__) • Software engineer @ COOKPAD Inc. •

    Development common libraries for Android / iOS
  3. We are hiring! https://info.cookpad.com/jobs/

  4. Comparison of Swift and Objective-C

  5. Swiftݴޠ࢓༷ͷৄࡉʹ͸৮Ε·ͤΜ

  6. SwiftͱObjective-Cͷൺֱ • ΞΫηε੍ޚ • ϝιουݺͼग़͠ߏจ • Φʔόʔϩʔυͱ໊લ෇͖Ҿ਺ • ૯শతϓϩάϥϛϯά

  7. ΞΫηε੍ޚ • Objective-C͸ެ։ϝιουͷΈϔομϑΝΠϧʹ ॻ͘ • ϑΟʔϧυʹ௚઀ΞΫηε͢Δͷ͸ݪଇېࢭ • Swift͸͢΂͕ͯpublicͰϑΟʔϧυΞΫηε΋ࣗ༝ • ΞΫηείϯτϩʔϧΛ͢Δʹ͸interfaceͱ࣮૷Ϋ

    ϥεΛ෼͚Δ͜ͱͰରԠͰ͖Δ͕࣮૷ίετ͕ߴ͍
  8. ϝιουݺͼग़͠ߏจ • Objective-C: [[receiver method1] method2] • Swift: receiver.method1().method2() •

    ϝιουνΣΠϯ͕΍Γ΍͍͢ • ࠷ޙͷҾ਺͕ؔ਺ܕͩͱಛघͳߏจ͋Γ • “trailing closures” • receiver.method() { params-> Type in … }
  9. Φʔόʔϩʔυͱ໊લ෇͖Ҿ਺ • γάωνϟ͕ҧ͑͹ಉ໊͡લͷϝιουΛෳ ਺ఆٛͰ͖Δ • ύϥϝλͷ਺΍ܕ͕ҧ͑͹OK • Ҿ਺ͷܕ͕ҧͬͯ΋OK • ໊લ෇͖ΦϓγϣφϧҾ਺΋ఆٛՄೳ

  10. ૯শతϓϩάϥϛϯά • ܕύϥϝʔλΛͱΔΫϥε΍ϝιουΛఆٛ Ͱ͖Δ • δΣωϦΫεͱ͔ςϯϓϨʔτͱ͔͍ΘΕ Δ΋ͷ • ͨͱ͑͹ Array<T>

    ͸ʮ೚ҙͷܕTͷ഑ྻʯ
  11. ͦͷଞ • Ϋϥεϝιουʢstatic methodʣ͸ଘࡏ͠ͳ͍ • ൃදͰ͸ఆٛͰ͖ͳ͍ͱݴͬͯ͠·͍·͕ͨ͠ɺ class funcͰఆٛͰ͖·͢ʢthanks to @corosuke_kʣ

    • ԋࢉࢠΦʔόʔϩʔυ͕͋Δ • nilΛσϑΥϧτͰڐ༰͠ͳ͍ • nilڐ༰ܕ͸ T? ͱ T!
  12. Swiftʹ͋Δ੍ݶ • ϦϑϨΫγϣϯAPI • ͦΕΒ͖͠API͸͋ΔͷͰɺਖ਼نϦϦʔε· Ͱʹ௥Ճ͞ΕΔՄೳੑ͸͋Γ • performSelectorܥͷಈతϝιουݺͼग़͠ • ObjCʹexportͨ͠Ϋϥεʹରͯ͠ͷΈՄೳ

  13. Interoperability of Swift and Objective-C

  14. ՝୊ • Objective-C੡ΞϓϦͰSwift੡ϥΠϒϥϦΛ࢖͑ Δ͔ɺ࢖͑Δͱͨ͠ΒͲ͏͍͏޻෉͕ඞཁ͔ • Swift੡ΞϓϦͰObjective-C੡ϥΠϒϥϦΛ࢖͑ Δ͔ɺ࢖͑Δͱͨ͠ΒͲ͏͍͏޻෉͕ඞཁ͔

  15. ૬ޓӡ༻ͷݪଇ • Swift͔ΒObjective-Cͷݺͼग़͠: Մೳ • Objective-C͔ΒSwiftͷݺͼग़͠: Մೳ • ͍ͣΕ΋ઃఆ͕ඞཁ •

    ΠϯλʔϑΣΠε͸100%ޓ׵Ͱ͸ͳ͍
  16. Objective-C from Swift • Objective-C APIΛSwift͔Β࢖͏ͨΊͷϒϦο δ͸Xcode͕ࣗಈతʹੜ੒ͯ͘͠ΕΔ • Swift͔Βݟ͑ΔCocoa APIͷ΄ͱΜͲ͸ͦͷ

    ··࢖͏͜ͱ͕Ͱ͖Δ • String, Array, Dictionary΋ObjCͦͷ··
  17. Blocks • ObjCͷblocks΋Swiftͷؔ਺ܕʹͳΔ • ϝιου΍ؔ਺ͷ࠷ޙͷҾ਺͕blocksͩͱɺ trailing closuresߏจ͕࢖͑Δ • RubyͰಋೖ͞Εͯ޿͘஌ΒΕΔΑ͏ʹͳͬ ͨϒϩοΫϝιουߏจͱ͓ͳ͡

  18. Example func dispatch_async(queue: dispatch_queue_t!, block: dispatch_block_t!) ! // trailing closures

    (຤ඌΫϩʔδϟʣ ! dispatch_async(dispatch_get_main_queue()) { NSLog(“foo”) } ! // ௨ৗͷߏจͰ΋࢖͑Δ ! dispatch_async(dispatch_get_main_queue(), { NSLog("foo") }) !
  19. Objective-CͷͨΊͷSwift • SwfitͰఆٛͨ͠ΫϥεΛObjective-CͰ࢖͏ͨ ΊͷϒϦοδ͸Xcode͕ࣗಈੜ੒͢Δ • @objc ΞϊςʔγϣϯΛ͚ͭΔɺ·ͨ͸ NSObjectΛܧঝͨ͠Ϋϥε͕ࣗಈతʹObjC ଆʹexport͞ΕΔ

  20. ஫ҙ఺ • ϝιουͷΦʔόʔϩʔυ΍ԋࢉࢠΦʔόʔ ϩʔυͳͲɺObjCͰ࣮ݱͰ͖ͳ͍ػೳ͸ΤΫ εϙʔτͰ͖ͳ͍ • ʮObjCରԠSwiftϥΠϒϥϦʯ͸ಛผͷରԠ͕ ඞཁͰɺͦΕʹΑͬͯSwiftຊདྷͷύϑΥʔϚ ϯεΛൃشͰ͖ͳ͍Մೳੑ͕͋Δ

  21. API Design

  22. Swift࣌୅ͷAPIઃܭ • Swift͸Objective-CΑΓڧྗ͕ͩɺશ໘తʹ SwiftʹҠߦ͢Δʹ͸·ͩ਺೥͔͔ΔͩΖ͏ • ͜Ε͔Βͷ਺೥ॏཁͳͷ͸ɺʮObjective-C͔ Β࢖͑ΔSwiftϥΠϒϥϦʯ͓ΑͼʮSwiftͰ΋ ࢖͍΍͍͢Objective-CϥΠϒϥϦʯͩʂ

  23. SwiftϥΠϒϥϦΛॻ͘ݪଇ • public classΛObjCʹexport͢Δ • 3จࣈϓϨϑΟΫε͸ґવͱͯ͠ඞཁͱࢥΘΕΔ • export͢ΔclassͰ͸੍ݶ͕͋Δ͜ͱʹ஫ҙ • ΦʔόʔϩʔυΛ࢖͑ͳ͍

    • δΣωϦΫεΛ࢖͑ͳ͍
  24. Objective-CΛॻ͘ݪଇ • มߋ఺͸ͦΕ΄Ͳͳ͍ • Cocoaࢿ࢈Λͦͷ··͔ͭ͏ͨΊʹ͔ͳΓ޻෉͍ͯ͠Δҹ৅ • ෳ਺ͷblocksΛड͚औΔͷͰ͸ͳ͘ɺ࠷ޙͷҾ਺ͰҰ͚ͭͩ blocksΛड͚औΔΑ͏ʹ͢Δ • ެ։APIͰͷϦϑϨΫγϣϯ͸ආ͚Δ

    • ͨͱ͑͹SELΛड͚औͬͯϝιουΛൃߦ͢ΔͷͰ͸ͳ͘ɺ blocksΛ࢖͏
  25. ஫ҙ • ਖ਼ن൛Ͱ͸طଘͷ੍ݶʹ͍ͭͯมߋ͕͋Δͱ ࢥΘΕΔͷͰɺಛʹϦϑϨΫγϣϯ΍ϒϦο δ·ΘΓ͸ݱࡏͷ࢓༷Λ৴͗͢͡ͳ͍΄͏͕ Α͍͔΋

  26. Show Notes • Welcome Swift • The Swfit Programming Language

    • Using Swift with Cocoa and Objective-C
  27. ͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠