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

Protocols in Swift

Yuya Hirayama
September 02, 2016

Protocols in Swift

Yuya Hirayama

September 02, 2016
Tweet

More Decks by Yuya Hirayama

Other Decks in Technology

Transcript

  1. Protocols in Swift
    Speee Engineer Meeting
    2016-08-31
    @hiragram

    View full-size slide

  2. SlackͷMac༻ΞϓϦ
    ॏ͘ͳ͍Ͱ͔͢ʁ

    View full-size slide

  3. • ͜ΜͳΞϓϦΛ࡞ͬ
    ͍ͯΔதͰߟ͑ͨ͜
    ͱΛ͠Ό΂Γ·͢

    View full-size slide

  4. ࿩͢͜ͱ
    • ܧঝͭΒ͍
    • Swiftͷϓϩτίϧʹ͍ͭͯ
    • ϓϩτίϧ͚ͩͰ݁߹ͨ͠ੈք
    • ϓϩτίϧ֦ு

    View full-size slide

  5. ܧঝͭΒ͍
    • Apple੡ͷϑϨʔϜϫʔΫ
    ͸ܧঝࡇΓ
    • શͯͷΫϥε͸NSObject
    ͷαϒΫϥε
    • ʮ͋ͷϝιουͬͯͲ͜ʹ
    ࣮૷͞Ε͚ͯͨͬʁʯ

    View full-size slide

  6. ܧঝͭΒ͍
    • UIViewControllerͷloadViewϝιου
    • ʮInterfaceBuilder࢖͏ͳΒΦʔόʔϥΠυ͠
    ͪΌμϝʯ

    View full-size slide

  7. ܧঝͭΒ͍
    • KVO(Ωʔ஋؂ࢹ)Ͱ࢖͏observeValueForKeyPathϝιου
    • ʮ਌ΫϥεͷΛݺͿͱ͖͸ͪΌΜͱ࣮૷͞ΕͯΔ͔͔֬Ίͯ
    Ͷʯ

    View full-size slide

  8. ϓϩτίϧࢦ޲ϓϩάϥϛϯά
    • ෳ਺Ϋϥεʹ·͕ͨͬͯ͋Δੑ࣭/ػೳΛ͍࣋ͨͤͨ
    ͱ͖͸ɺܧঝʹΑͬͯͰ͸ͳ͘ɺϓϩτίϧʹΑͬͯ
    ࣮ݱ͠·͠ΐ͏ɻ(BaseViewControllerͱ͔ͭΒ͍)
    • ܧঝ͸Ұͭͷ਌͔࣋ͯ͠ͳ͍͕ɺϓϩτίϧͳΒෳ਺
    ࣋ͯͯ͏Ε͍͠ɻ
    • ෳ਺࣋ͯΔ͕ɺෳ਺ͷϓϩτίϧͰ໊લ͕িಥ͢Δ
    ͱશͯʹద߹Ͱ͖͍ͯͳ͍ͱ͍͏͜ͱͰίϯύΠϧ
    ௨Βͳ͍ɻ͜Ε͸ෆՄආɻ

    View full-size slide

  9. Swiftͷϓϩτίϧ
    • Property / Method Requirements
    • ࣮૷Λڧ੍͢Δ
    • Associatedtype
    • ϓϩτίϧ಺Ͱར༻͢Δɺؔ࿈͢Δܕ
    • Protocol Extension
    • ޙड़

    View full-size slide

  10. ม਺ͷܕΛϓϩτίϧͰଋറ͢Δ

    View full-size slide

  11. ม਺ͷܕΛϓϩτίϧͰଋറ͢Δ
    • a͸ʮAwesomeProtocolʹ४ڌͯ͠ΔܕͷΦϒ
    δΣΫτ͸ԿͰ΋ಥͬࠐΊΔʯ
    • b͸ʮAwesomeProtocolʹ४ڌ͍ͯ͠Δͻͱͭͷ
    ܕͷΦϒδΣΫτ͚ͩಥͬࠐΊΔʯ
    • ܕਪ࿦ͷ͓͔͛ͰδΣωϦΫεͱͯ΋࢖͍΍͍͢

    View full-size slide

  12. ࣗ࡞SlackΞϓϦͰ΍ͬͯΔ͜ͱ
    • ςʔϒϧʹදࣔ͢ΔϝοηʔδͷܕΛ௚઀ࢦఆ͠
    ͍͕ͯͨɺϑΟʔυʹදࣔ͢ΔΠϕϯτ༻ͷϓϩ
    τίϧΛ࡞ͬͨ → ϝοηʔδҎ֎ͷΠϕϯτ΋
    ؆୯ʹදࣔͰ͖ΔΑ͏ʹͳͬͨ

    View full-size slide

  13. ௚઀ܕʹ݁߹ͨ͠ੈք
    7JFX$POUSPMMFS .FTTBHF&OUJUZ

    View full-size slide

  14. ௚઀ܕʹ݁߹ͨ͠ੈք
    7JFX$POUSPMMFS

    View full-size slide

  15. ਌Ϋϥεʹ݁߹ͨ͠ੈք
    7JFX$POUSPMMFS ͳΜͨΒ
    .FTTBHF&OUJUZ

    View full-size slide

  16. ਌Ϋϥεʹ݁߹ͨ͠ੈք
    7JFX$POUSPMMFS

    ͳΜͨΒ
    .FTTBHF&OUJUZ

    View full-size slide

  17. ϓϩτίϧ͚ͩͰ݁߹ͨ͠ੈք
    7JFX$POUSPMMFS .FTTBHF&OUJUZ
    "XFTPNF1SPUPDPM

    View full-size slide

  18. ϓϩτίϧ͚ͩͰ݁߹ͨ͠ੈք
    7JFX$POUSPMMFS "XFTPNF1SPUPDPM

    View full-size slide

  19. ϓϩτίϧ͚ͩͰ݁߹ͨ͠ੈք
    7JFX$POUSPMMFS "XFTPNF1SPUPDPM

    ผͷ&OUJUZ

    View full-size slide

  20. ϓϩτίϧ͚ͩͰ݁߹ͨ͠ੈք
    ͷ޾ͤͳͱ͜Ζ
    • ݺͼग़͠ଆ(͖ͬ͞ͷྫͰ͸ViewController)͕஌ͬ
    ͍ͯΔϝιου͕ݶఆ͞ΕΔ
    • ʮ͜ͷϝιου௚ͦ͏ͱࢥ͚ͬͨͲɺ૝ఆ֎ͷॴ͔
    Βݺ͹Εͯͯม͑ΒΕͳ͍ʯΛ(͋Δఔ౓)ճආͰ͖
    Δ
    • ϓϩτίϧʹద߹͍ͯ͑͠͞Ε͹͍͍ͷͰɺଞͷܕ
    ͱೖΕସ͑ͨΓ৽͘͠௥Ճͨ͠Γ͢Δͷָ͕͍͢͝

    View full-size slide

  21. ϓϩτίϧ͚ͩͰ݁߹ͨ͠ੈք
    ͷͭΒ͍ͱ͜Ζ
    • ܕΛࢦఆͯ͠ಈ͘ͱ͖ΑΓϓϩτίϧ͔ΒΠϯελ
    ϯεʹΞΫηε͢Δͱ͖ॲཧ͕஗͍
    • Xcodeͷʮఆٛ΁δϟϯϓʯ͕ͪΐͬͱෆศʹͳΔ
    • ϓϩτίϧ͕ཚཱͯ͘͠Δͱ໊લͱ͔ͪΌΜͱ෇͚
    ͓͔ͯͳ͍ͱޙͰۤͦ͠͏ʁ
    • delegateͷதͰ͞Βʹdelegateͱ͔΍ͬͯΔͱΧ
    Φε

    View full-size slide

  22. ϓϩτίϧ֦ு
    • Swift2͔Β௥Ճ͞Εͨϓϩτίϧ֦ு(Protocol
    Extension)
    • ࣮૷Λڧ੍͢ΔProtocolʹରͯ͠ɺσϑΥϧτ
    ͷ࣮૷ΛఆٛͰ͖Δɻ

    View full-size slide

  23. femitΞϓϦʹ
    Ұॠ͋ͬͨίʔυ

    View full-size slide

  24. ϓϩτίϧ֦ுΛ࢖͏ͱ
    • σϑΥϧτ࣮૷͚ͩͰ͍͍࣌͸ɺܕͷఆٛʹϓ
    ϩτίϧ໊Λॻ͖଍͚ͩ͢Ͱͦͷϓϩτίϧʹ
    ద߹Ͱ͖Δɻָɻ
    • ͋Δಛఆͷܕʹରͯ͠ϓϩτίϧ੍໿Λ͚ͭΔ
    ͱ͖͚ͩར༻ՄೳͳσϑΥϧτ࣮૷ΛఆٛͰ͖
    Δ

    View full-size slide

  25. ·ͱΊ
    • ݺͼग़͠ݩ͔Βݟ͑Δൣғ͕ݶఆͰ͖Δͷྑ͍
    • δΣωϦΫε+ܕਪ࿦ͰΧνοͱ͍͔ͯͯͭ͠ίʔ
    υ͸ॻ͖΍͍͢
    • ܧঝͭΒ͍
    • ࢀরܕͭΒ͍ͱ͍͏จ຺ͱ߹ΘͤͯΫϥεͰ͸ͳ
    ͘஋ܕͰܧঝͰ͖ͳ͍ߏ଄ମΛ࢖͓͏ͥΈ͍ͨͳ
    ෩ை͕͋Δ

    View full-size slide