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

20180410_iOSLT_SwiftとProtocol-OrientedProgramming

 20180410_iOSLT_SwiftとProtocol-OrientedProgramming

shtnkgm

April 10, 2018
Tweet

More Decks by shtnkgm

Other Decks in Programming

Transcript

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

    } class Cat { func eat() { /* something */ } } class Dog { func eat() { /* something */ } } class Bird { func eat() { /* something */ } }
  2. class Animal { func eat() { /* something */ }

    } class Fish: Animal { } class Cat: Animal { } class Dog: Animal { } class Bird: Animal { }
  3. class Animal { func eat() { /* something */ }

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

    func swim() { /* something */ } func run() { /* something */ } func fly() { /* something */ } } class Dog: Animal { } let dog = Dog() dog.fly()
  5. ΑΓ҆શͳϓϩάϥϜʹ class Animal { func eat() { /* something */

    } } class RunAnimal: Animal { func run() { /* something */ } } class Dog: RunAnimal { } let dog = Dog() dog.run() // OK dog.fly() // ίϯύΠϧΤϥʔ
  6. Ϋϥεͷܧঝʹؔ͢Δ໰୊ᶅ • ந৅֓೦΋ΠϯελϯεԽͰ͖ͯ͠·͏ →όάͷݪҼ // ۩৅ΫϥεͷΠϯελϯεԽ let dog = Dog()

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

    ಈ෺ͱ͍͏֓೦͸ͳΜͯ໐͘?? // ΦʔόʔϥΠυ͞Εͳ͍৔߹͸Ϋϥογϡͤ͞Δ͔... fatalError() } } ܧঝઌͰͷ࣮૷࿙Ε͕͋Δͱόάͷݩʹ
  8. protocol Eatable { func eat() } struct Cat: Eatable {

    // eatϝιουͷ࣮૷͕ڧ੍͞ΕΔ // ίϯύΠϥͰνΣοΫ͞ΕΔͷͰ҆શ func eat() { print("ͺ͘ͺ͘") } }
  9. ϓϩτίϧΤΫεςϯγϣϯͰσϑΥϧτ࣮૷ protocol Eatable { func eat() } extension Eatable {

    func eat() { print("΋͙΋͙") } } struct Dog: Eatable { } let dog = Dog() dog.eat() // ΋͙΋͙
  10. 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() // ӭ͍ͰਐΈ·͢
  11. ϓϩτίϧ͸ܕͱͯ͠ѻ͑Δ • ϙϦϞʔϑΟζϜͰૄ݁߹ʹ ! func feed(eatable: Eatable) • ΠϯελϯεԽෆՄ !

    // Error: Protocol type 'Eatable' cannot be instantiated let eatable = Eatable()
  12. protocol Eatable { associatedtype FoodType func eat(food: FoodType) } struct

    Bird: Eatable { typealias FoodType = Fish func eat(food: FoodType) { // do something } }
  13. ΦϒδΣΫτࢦ޲ ϓϩτίϧࢦ޲ σʔλͷ҉໧తڞ༗ ×ʢ͋Γʣ ◦ʢͳ͠ʣ ந৅Խ ◦ʢSuperclassʣ ◦ʢProtocolʣ ίϯϙδγϣϯ ×ʢͰ͖ͳ͍ʣ

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

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