20180410_iOSLT_SwiftとProtocol-OrientedProgramming

 20180410_iOSLT_SwiftとProtocol-OrientedProgramming

E45f9c343d90c74554c65c89c6f861bc?s=128

shtnkgm

April 10, 2018
Tweet

Transcript

  1. SwiftͱProtocol-Oriented Programming 2018.4.10 Shota Nakagami / @shtnkgm

  2. ຊ೔ͷΰʔϧ ϓϩτίϧࢦ޲Λ஌ͬͯɺ΋ͬͱSwiftyͳΤϯδχΞʹ

  3. "Swift is a protocol-oriented language. " — David Abrahams @WWDC2015

  4. Google Trendsʮprotocol-oriented programmingʯ

  5. ϓϩτίϧΦϦΤϯςουͬͯͳΜͩΖ͏ʁ !

  6. Protocol-Oriented Programming: POP • ༁͢ͱϓϩτίϧࢦ޲ϓϩάϥϛϯά • ϓϩτίϧࢦ޲ϓϩάϥϛϯά͸ϓϩάϥϛϯάύϥμΠϜͷ Ұͭ

  7. ϓϩάϥϛϯάύϥμΠϜͬͯͳΜͩΖ͏ʁ !

  8. Programming Paradigm • ϓϩάϥϛϯάΛ͢Δͱ͖ͷߟ͑ํɺํ๏࿦ͷ͜ͱ • ҰͭͷݴޠͰෳ਺ͷύϥμΠϜʹ෼ྨ͞ΕΔ͜ͱ΋͋Δ ʢ= ϚϧνύϥμΠϜʣ

  9. ϓϩάϥϛϯάύϥμΠϜͷҰྫ ϓϩάϥϛϯάύϥμΠϜ ֓ཁ खଓ͖ܕʢProceduralʣ ঢ়ଶΛ࣋ͪɺҰ࿈ͷॲཧΛؔ਺ʹ·ͱΊΔ ΦϒδΣΫτࢦ޲ʢObject-Orientedʣ σʔλ΍ৼΔ෣͍ΛΦϒδΣΫτʹ࣋ͨͤΔ ؔ਺ܕʢFunctionalʣ ঢ়ଶ΍෭࡞༻Λ࣋ͨͳ͍ؔ਺Λ૊Έ߹ΘͤΔ ϦΞΫςΟϒʢReactiveʣ

    σʔλͷྲྀΕʢdata flowʣͱσʔλͷมԽͷ ఻೻ʹண໨ δΣωϦοΫʢGenericʣ σʔλܗࣜʹґଘ͠ͳ͍ ΞεϖΫτࢦ޲ʢAspect-Orientedʣ ԣஅతؔ৺ࣄͷ෼཭ʹண໨
  10. iOSΞϓϦ։ൃ͸ϚϧνύϥμΠϜ • Objective-CɾɾɾΦϒδΣΫτࢦ޲ • Swiftɾɾɾϓϩτίϧࢦ޲ ʢΦϒδΣΫτࢦ޲&ؔ਺ܕ&δΣωϦοΫʣ • ಛʹɺUIKit͸ΦϒδΣΫτࢦ޲ͱͷؔΘΓ͕ڧ͍

  11. ΦϒδΣΫτࢦ޲ͬͯͳΜ͚ͩͬʁ

  12. ΦϒδΣΫτࢦ޲ͷ͓͞Β͍

  13. Object-Oriented Programming: OOP • σʔλ΍ϝιουΛ࣋ͬͨΦϒδΣΫτΛͭͬͯͦ͘ͷ૬ޓ࡞ ༻ͰγεςϜΛܗ࡞Δ • ΦϒδΣΫτࢦ޲ͷࡾݪଇ • ϙϦϞʔϑΟζϜʢଟଶੑʣ

    • ΧϓηϧԽʢӅṭʣ • ܧঝ
  14. UIKitͷΫϥε֊૚

  15. None
  16. ΦϒδΣΫτࢦ޲ͱϓϩτίϧࢦ޲

  17. Swift Standard Framework͸΄΅ߏ଄ମͰߏ੒ ओͳߏ੒ཁૉ ΦϒδΣΫτࢦ޲ ϓϩτίϧࢦ޲ ܕ ࢀরܕʢClassʣ ஋ܕʢStruct/Enumʣ ந৅Խ

    Superclass Protocol →Obective-C࣌୅ͷΫϥεͷར༻͔Βߏ଄ମͷར༻͕த৺ʹ
  18. ΦϒδΣΫτࢦ޲Ͱʮಈ෺ʯΛσβΠϯͯ͠ΈΔ

  19. ͍ΖΜͳಈ෺ΫϥεΛͭ͘Γ·͢

  20. class Fish { } class Cat { } class Dog

    { } class Bird { }
  21. Ͳͷಈ෺΋͝͸ΜΛ৯΂Δ →ॲཧΛڞ௨Խ͍ͨ͠

  22. class Fish { func eat() { /* something */ }

    } class Cat { func eat() { /* something */ } } class Dog { func eat() { /* something */ } } class Bird { func eat() { /* something */ } }
  23. ਌Ϋϥεʹڞ௨ॲཧΛ࣮૷ͯ͠ܧঝ

  24. class Animal { func eat() { /* something */ }

    } class Fish: Animal { } class Cat: Animal { } class Dog: Animal { } class Bird: Animal { }
  25. Ҡಈํ๏΋·ͱΊΒΕͦ͏ʁ

  26. ਌Ϋϥεʹ࣮૷ʁ

  27. class Animal { func eat() { /* something */ }

    func swim() { /* something */ } func run() { /* something */ } func fly() { /* something */ } } class Dog: Animal { } let dog = Dog() dog.fly()
  28. class Animal { func eat() { /* something */ }

    func swim() { /* something */ } func run() { /* something */ } func fly() { /* something */ } } class Dog: Animal { } let dog = Dog() dog.fly()
  29. ۭඈͿ͍͵͕ൃੜʢόάʣ

  30. Ҡಈํ๏ʹ͍ͭͯͷந৅֓೦Λ࣮૷

  31. ΑΓ҆શͳϓϩάϥϜʹ class Animal { func eat() { /* something */

    } } class RunAnimal: Animal { func run() { /* something */ } } class Dog: RunAnimal { } let dog = Dog() dog.run() // OK dog.fly() // ίϯύΠϧΤϥʔ
  32. ʊਓਓਓਓਓਓਓਓਓʊ ʼɹಥવͷ࢓༷มߋɹʻ ʉY^Y^Y^Y^Y^Y^Y^Yʉ

  33. None
  34. ӭ͛ͯ૸ΕΔΧΤϧ͸ʁ

  35. None
  36. Ϋϥε͸ଟॏܧঝΛαϙʔτ͍ͯ͠ͳ͍ // error: multiple inheritance from classes 'SwimAnimal' and 'RunAnimal'

    class Frog: SwimAnimal, RunAnimal { }
  37. Ϋϥεͷܧঝʹؔ͢Δ໰୊ᶃ • ୯Ұʢ୯७ʣܧঝ͔͠Ͱ͖ͳ͍ͷͰɺΫϥεઃܭ͕೉͍͠ ɹˠ਌Ϋϥε͕ංେԽɺڽू౓͕௿͘ͳΓ͕ͪʢͪ͝Ό·ͥʣ

  38. Ϋϥεͷܧঝʹؔ͢Δ໰୊ᶄ • ਌Ϋϥεͷมߋ͕શͯͷܧঝઌΫϥεʹ೾ٴ͢Δ ɹ →όάͷݪҼɺ਌Ϋϥε͸มߋ͠ʹ͍͘

  39. Ϋϥεͷܧঝʹؔ͢Δ໰୊ᶅ • ந৅֓೦΋ΠϯελϯεԽͰ͖ͯ͠·͏ →όάͷݪҼ // ۩৅ΫϥεͷΠϯελϯεԽ let dog = Dog()

    let frog = Frog() // ந৅ΫϥεͷΠϯελϯεԽ let animal = Animal() // ͲΜͳಈ෺ʁ
  40. Ϋϥεͷܧঝʹؔ͢Δ໰୊ᶆ • ΦʔόʔϥΠυલఏͷϝιου class Animal { func bark() { //

    ಈ෺ͱ͍͏֓೦͸ͳΜͯ໐͘?? // // } }
  41. Ϋϥεͷܧঝʹؔ͢Δ໰୊ᶆ • ΦʔόʔϥΠυલఏͷϝιου class Animal { func bark() { //

    ಈ෺ͱ͍͏֓೦͸ͳΜͯ໐͘?? // ΦʔόʔϥΠυ͞Εͳ͍৔߹͸Ϋϥογϡͤ͞Δ͔... fatalError() } } ܧঝઌͰͷ࣮૷࿙Ε͕͋Δͱόάͷݩʹ
  42. ϓϩτίϧࢦ޲Ͱʮಈ෺ʯΛσβΠϯͯ͠ΈΔ

  43. Protocol Conformance ϓϩτίϧ४ڌʢద߹ʣ

  44. ੑ࣭ΛϓϩτίϧͰදݱͯ͠४ڌ

  45. protocol Eatable { func eat() } struct Cat: Eatable {

    // eatϝιουͷ࣮૷͕ڧ੍͞ΕΔ // ίϯύΠϥͰνΣοΫ͞ΕΔͷͰ҆શ func eat() { print("ͺ͘ͺ͘") } }
  46. Protocol Extension ϓϩτίϧΤΫεςϯγϣϯʢ֦ுʣ

  47. ϓϩτίϧΤΫεςϯγϣϯͰσϑΥϧτ࣮૷ protocol Eatable { func eat() } extension Eatable {

    func eat() { print("΋͙΋͙") } } struct Dog: Eatable { } let dog = Dog() dog.eat() // ΋͙΋͙
  48. Protocol Composition ϓϩτίϧίϯϙδγϣϯʢ߹੒ʣ

  49. ෳ਺ϓϩτίϧ४ڌͰΧΤϧ໰୊ղফ

  50. struct Frog: Swimmable, Runnable { } let frog = Frog()

    frog.swim() frog.run()
  51. Protocol Inheritance ϓϩτίϧܧঝ

  52. ϓϩτίϧܧঝ΋Մೳ

  53. protocol Movable { func move(method: String) } extension Movable {

    func move(method: String) { print(method + "ਐΈ·͢") } } protocol Swimmable: Movable { func swim() } extension Swimmable { func swim() { move(method: "ӭ͍Ͱ") } } struct Fish: Swimmable { } let fish = Fish() fish.swim() // ӭ͍ͰਐΈ·͢
  54. ͞ΒʹFlyableΛಈ෺Ͱͳ͍ܕͰ४ڌ • e.g. ඈߦػ • ඈͿͱ͍͏ੑ࣭͔Βઃܭ͢Δ͜ͱͰɺಈ෺ʹ΋ґଘ͠ͳ͍ • ॊೈͰ֦ுੑͷ͋ΔϓϩάϥϜʹͳΔ

  55. ϓϩτίϧ͸஋ܕͰ΋࢖͑Δ • ஋ܕͷStruct΍Enumʹ͸ΫϥεͷΑ͏ͳܧঝػೳ͸ͳ͍͕ɺϓ ϩτίϧʹ͸४ڌͰ͖Δ • ஋ܕ͸ࢀরܕʢΫϥεʣΑΓ΋҆શ • ஋Λڞ༗͠ͳ͍ͷͰҙਤ͠ͳ͍஋ͷมߋ͕ൃੜ͠ͳ͍ • εϨουηʔϑ

    • ॥؀ࢀর໰୊͕ൃੜ͠ͳ͍
  56. ϓϩτίϧ͸ܕͱͯ͠ѻ͑Δ • ϙϦϞʔϑΟζϜͰૄ݁߹ʹ ! func feed(eatable: Eatable) • ΠϯελϯεԽෆՄ !

    // Error: Protocol type 'Eatable' cannot be instantiated let eatable = Eatable()
  57. δΣωϦΫεʢ૯শܕʣ΋࢖͑Δ Ϋϥε΍StructͷΑ͏ͳܕύϥϝʔλ<T>͸࢖͑ͳ͍͕ɺ associatedtypeͰ૯শܕΛఆٛͰ͖Δ class Eatable<FoodType> { } struct Eatable<FoodType> {

    } protocol Eatable { associatedtype FoodType }
  58. protocol Eatable { associatedtype FoodType func eat(food: FoodType) } struct

    Bird: Eatable { typealias FoodType = Fish func eat(food: FoodType) { // do something } }
  59. ϓϩτίϧࢦ޲͍͍Ͷ

  60. ·ͱΊ

  61. ࠷ڧͷϓϩάϥϛϯάύϥμΠϜ͸ͳ͍ • ͲΕ΋໰୊ղܾͷखஈͷҰͭʹ͗͢ͳ͍ • ϓϩτίϧࢦ޲Λ஌Δ͜ͱͰϓϩάϥϛϯάͷ෯͕޿͕Δ • ͍ΖΜͳํ๏࿦Λ஌Γɺదࡐదॴ࢖͑Δͷ͕࠷ڧ

  62. ΦϒδΣΫτࢦ޲ ϓϩτίϧࢦ޲ σʔλͷ҉໧తڞ༗ ×ʢ͋Γʣ ◦ʢͳ͠ʣ ந৅Խ ◦ʢSuperclassʣ ◦ʢProtocolʣ ίϯϙδγϣϯ ×ʢͰ͖ͳ͍ʣ

    ◦ʢProtocol Compositionʣ ந৅ܕͷΠϯελϯεԽ ×ʢͰ͖Δʣ ◦ʢͰ͖ͳ͍ʣ σϑΥϧτ࣮૷ ◦ʢSuperclassʣ ◦ʢProtocol Extensionʣ ۩৅ܕ΁ͷ࣮૷ͷڧ੍ ×ʢಈతνΣοΫʣ ◦ʢ੩తνΣοΫʣ ϙϦϞʔϑΟζϜ ◦ʢSuperclassʣ ◦ʢProtocolʣ δΣωϦΫε ◦ʢܕύϥϝʔλʣ ◦ʢassociatedtypeʣ UIKitΦϒδΣΫτͷܧঝ ◦ʢͰ͖Δʣ ×ʢͰ͖ͳ͍ʣ ϥΠϑαΠΫϧ؅ཧʢdeinitʣ ◦ʢ͋Γʣ ×ʢͳ͠ʣ Ұҙੑͷ͋Δσʔλʢsingletonʣ ◦ʢ࡞Γ΍͍͢ʣ ×ʢ࡞Γʹ͍͘ʣ
  63. ΑΓৄ͘͠ • WWDC2015ηογϣϯಈը Protocol-Oriented Programming in Swift / WWDC2015 •

    ެࣜϦϑΝϨϯε Protocols / The Swift Programming Language (Swift 4.1) • ॻ੶ Swift 4 Protocol-Oriented Programming - Third Edition
  64. ͓ΘΓ