Protocol と Class と SubClass

Af64bc38c0ffcfcabdf430759ee491ce?s=47 Elvis Shi
August 08, 2017

Protocol と Class と SubClass

Protocol で宣言したメソッドが Class / SubClass での動き

Af64bc38c0ffcfcabdf430759ee491ce?s=128

Elvis Shi

August 08, 2017
Tweet

Transcript

  1. Protocol ͱ Class ͱ SubClass for Otemachi.swift x Kyobashi.swift #01

  2. override init() { super.init() emplyedBy = "MAGES. 5pb. Game div"

    job = "iOS Developer…?" twitter = "@lovee" qiita = "@lovee" github = "el-hoshino" additionalInfo = "Unity ਏ͍ʘ(^o^)ʗ" class Speaker: Developer { } }
  3. খωλ protocol SomeProtocol { func printSelf() } extension SomeProtocol {

    func printSelf() { print("SomeProtocol") } } protocol SomeProtocol { } extension SomeProtocol { func printSelf() { print("SomeProtocol") } } ≠
  4. খωλ protocol SomeProtocol { func printSelf() } extension SomeProtocol {

    func printSelf() { print("SomeProtocol") } } class SomeClass: SomeProtocol { func printSelf() { print("SomeClass") } } let sc = SomeClass() sc.printSelf() // SomeClass (sc as SomeProtocol).printSelf() // SomeClass protocol SomeProtocol { } extension SomeProtocol { func printSelf() { print("SomeProtocol") } } class SomeClass: SomeProtocol { func printSelf() { print("SomeClass") } } let sc = SomeClass() sc.printSelf() // SomeClass (sc as SomeProtocol).printSelf() // SomeProtocol ≠
  5. খωλ protocol SomeProtocol { func printSelf() } extension SomeProtocol {

    func printSelf() { print("SomeProtocol") } } class SomeClass: SomeProtocol { func printSelf() { print("SomeClass") } } let sc = SomeClass() sc.printSelf() // SomeClass (sc as SomeProtocol).printSelf() // SomeClass protocol SomeProtocol { } extension SomeProtocol { func printSelf() { print("SomeProtocol") } } class SomeClass: SomeProtocol { func printSelf() { print("SomeClass") } } let sc = SomeClass() sc.printSelf() // SomeClass (sc as SomeProtocol).printSelf() // SomeProtocol ≠ Protocol ͷ Interface (PI) એݴ PI σϑΥϧτ࣮૷ PI ຊ࣮૷ PI ݺͼग़͠ PI ݺͼग़͠ Protocol ͷ Interface (PI) એݴͳ͠ Protocol ͷ Method (PM) ࣮૷ Class ͷ Method (CM) ࣮૷ CM ݺͼग़͠ PM ݺͼग़͠
  6. খωλ ⬆ৄ͘͠͸⬆ http://qiita.com/HaNoHito/items/f40bfc1717c1e922a5b0 https://speakerdeck.com/nhamada/swiftniokerumethod-dispatchnituite

  7. খωλ protocol SomeProtocol { func printSelf() } extension SomeProtocol {

    func printSelf() { print("SomeProtocol") } } class SomeClass: SomeProtocol { func printSelf() { print("SomeClass") } } let sc = SomeClass() sc.printSelf() // SomeClass (sc as SomeProtocol).printSelf() // SomeClass protocol SomeProtocol { } extension SomeProtocol { func printSelf() { print("SomeProtocol") } } class SomeClass: SomeProtocol { func printSelf() { print("SomeClass") } } let sc = SomeClass() sc.printSelf() // SomeClass (sc as SomeProtocol).printSelf() // SomeProtocol ≠ Protocol ͷ Interface (PI) એݴ PI σϑΥϧτ࣮૷ PI ຊ࣮૷ PI ݺͼग़͠ PI ݺͼग़͠ Protocol ͷ Interface (PI) એݴͳ͠ Protocol ͷ Method (PM) ࣮૷ Class ͷ Method (CM) ࣮૷ CM ݺͼग़͠ PM ݺͼग़͠
  8. ຊ୊ protocol SomeProtocol { func printSelf() } extension SomeProtocol {

    func printSelf() { print("SomeProtocol") } } class SomeClass: SomeProtocol { func printSelf() { print("SomeClass") } } let sc = SomeClass() sc.printSelf() // SomeClass (sc as SomeProtocol).printSelf() // SomeClass
  9. ຊ୊ protocol SomeProtocol { func printSelf() } extension SomeProtocol {

    func printSelf() { print("SomeProtocol") } } class SomeClass: SomeProtocol { }
  10. ຊ୊ protocol SomeProtocol { func printSelf() } extension SomeProtocol {

    func printSelf() { print("SomeProtocol") } } class SomeClass: SomeProtocol { } class SomeSubClass: SomeClass { func printSelf() { print("SomeSubClass") } } class Parent { let someClass: SomeClass // init(someClass: SomeClass) { ... } func printSelf() { someClass.printSelf() } } let ssc = SomeSubClass() let p = Parent(someClass: ssc) ssc.printSelf() // SomeSubClass p.printSelf() // ?
  11. ຊ୊ protocol SomeProtocol { func printSelf() } extension SomeProtocol {

    func printSelf() { print("SomeProtocol") } } class SomeClass: SomeProtocol { } class SomeSubClass: SomeClass { func printSelf() { print("SomeSubClass") } } class Parent { let someClass: SomeClass // init(someClass: SomeClass) { ... } func printSelf() { someClass.printSelf() } } let ssc = SomeSubClass() let p = Parent(someClass: ssc) ssc.printSelf() // SomeSubClass p.printSelf() // ? "ग़ྗ͸ʁ AɿSomeProtocol BɿSomeClass CɿSomeSubClass
  12. ຊ୊ protocol SomeProtocol { func printSelf() } extension SomeProtocol {

    func printSelf() { print("SomeProtocol") } } class SomeClass: SomeProtocol { } class SomeSubClass: SomeClass { func printSelf() { print("SomeSubClass") } } class Parent { let someClass: SomeClass // init(someClass: SomeClass) { ... } func printSelf() { someClass.printSelf() } } let ssc = SomeSubClass() let p = Parent(someClass: ssc) ssc.printSelf() // SomeSubClass p.printSelf() // SomeProtocol "ग़ྗ͸ʁ
 
 AɿSomeProtocol#
 BɿSomeClass$
 CɿSomeSubClass$
  13. ຊ୊ protocol SomeProtocol { func printSelf() } extension SomeProtocol {

    func printSelf() { print("SomeProtocol") } } class SomeClass: SomeProtocol { } class SomeSubClass: SomeClass { func printSelf() { print("SomeSubClass") } } class Parent { let someClass: SomeClass // init(someClass: SomeClass) { ... } func printSelf() { someClass.printSelf() } } let ssc = SomeSubClass() let p = Parent(someClass: ssc) p.printSelf() // SomeProtocol // == (ssc as SomeClass).printSelf() // == (ssc as SomeProtocol).printSelf() Protocol ͷ Interface (PI) એݴ PI ຊ࣮૷ͳ͠ʢσϑΥϧτ࣮૷࢖༻ʣ PI ຊ࣮૷ʢσϑΥϧτ࣮૷ʣݺͼग़͠ PI ຊ࣮૷ʢσϑΥϧτ࣮૷ʣݺͼग़͠ Class (SomeSubClass) ͷ Method ࣮૷
  14. p.printSelf()Ͱ
 SomeSubClass Λ
 ग़ྗͤ͞Δʹ͸ʁ

  15. ຊ୊ protocol SomeProtocol { func printSelf() } extension SomeProtocol {

    func printSelf() { print("SomeProtocol") } } class SomeClass: SomeProtocol { } class SomeSubClass: SomeClass { func printSelf() { print("SomeSubClass") } } class Parent { let someClass: SomeClass // init(someClass: SomeClass) { ... } func printSelf() { someClass.printSelf() } } let ssc = SomeSubClass() let p = Parent(someClass: ssc) p.printSelf() // SomeProtocol // == (ssc as SomeClass).printSelf() // == (ssc as SomeProtocol).printSelf()
  16. ຊ୊ protocol SomeProtocol { func printSelf() } extension SomeProtocol {

    func printSelf() { print("SomeProtocol") } } class SomeClass: SomeProtocol { } class SomeSubClass: SomeClass { func printSelf() { print("SomeSubClass") } } class Parent { let someClass: SomeClass // init(someClass: SomeClass) { ... } func printSelf() { someClass.printSelf() } } let ssc = SomeSubClass() let p = Parent(someClass: ssc) p.printSelf() // SomeProtocol // == (ssc as SomeClass).printSelf() // == (ssc as SomeProtocol).printSelf() protocol SomeProtocol { func printSelf() } extension SomeProtocol { func printSelf() { print("SomeProtocol") } } class SomeClass: SomeProtocol { } class SomeSubClass: SomeClass { func printSelf() { print("SomeSubClass") } } class Parent { let someClass: SomeClass // init(someClass: SomeClass) { ... } func printSelf() { someClass.printSelf() } } let ssc = SomeSubClass() let p = Parent(someClass: ssc) p.printSelf() // SomeProtocol // == (ssc as SomeClass).printSelf() // == (ssc as SomeProtocol).printSelf()
  17. ຊ୊ protocol SomeProtocol { func printSelf() } extension SomeProtocol {

    func printSelf() { print("SomeProtocol") } } class SomeClass: SomeProtocol { } class SomeSubClass: SomeClass { func printSelf() { print("SomeSubClass") } } class Parent { let someClass: SomeClass // init(someClass: SomeClass) { ... } func printSelf() { someClass.printSelf() } } let ssc = SomeSubClass() let p = Parent(someClass: ssc) p.printSelf() // SomeProtocol // == (ssc as SomeClass).printSelf() // == (ssc as SomeProtocol).printSelf() protocol SomeProtocol { func printSelf() } extension SomeProtocol { func printSelf() { print("SomeProtocol") } } class SomeClass: SomeProtocol { func printSelf() { (self as SomeProtocol).printSelf() } } class SomeSubClass: SomeClass { override func printSelf() { print("SomeSubClass") } } class Parent { let someClass: SomeClass // init(someClass: SomeClass) { ... } func printSelf() { someClass.printSelf() } } let ssc = SomeSubClass() let p = Parent(someClass: ssc) p.printSelf() // SomeSubClass // == (ssc as SomeClass).printSelf() // == (ssc as SomeProtocol).printSelf()
  18. ຊ୊ protocol SomeProtocol { func printSelf() } extension SomeProtocol {

    func printSelf() { print("SomeProtocol") } } class SomeClass: SomeProtocol { } class SomeSubClass: SomeClass { func printSelf() { print("SomeSubClass") } } class Parent { let someClass: SomeClass // init(someClass: SomeClass) { ... } func printSelf() { someClass.printSelf() } } let ssc = SomeSubClass() let p = Parent(someClass: ssc) p.printSelf() // SomeProtocol // == (ssc as SomeClass).printSelf() // == (ssc as SomeProtocol).printSelf() protocol SomeProtocol { func printSelf() } extension SomeProtocol { func printSelf() { print("SomeProtocol") } } class SomeClass: SomeProtocol { func printSelf() { (self as SomeProtocol).printSelf() } } class SomeSubClass: SomeClass { override func printSelf() { print("SomeSubClass") } } class Parent { let someClass: SomeClass // init(someClass: SomeClass) { ... } func printSelf() { someClass.printSelf() } } let ssc = SomeSubClass() let p = Parent(someClass: ssc) p.printSelf() // SomeSubClass // == (ssc as SomeClass).printSelf() // == (ssc as SomeProtocol).printSelf() Protocol ͷ Interface (PI) એݴ PI ຊ࣮૷ͳ͠ʢσϑΥϧτ࣮૷࢖༻ʣ Class (SomeSubClass) ͷ Method ࣮૷ PI ຊ࣮૷ʢσϑΥϧτ࣮૷ʣݺͼग़͠ PI ຊ࣮૷ʢσϑΥϧτ࣮૷ʣݺͼग़͠ Protocol ͷ Interface (PI) એݴ PI ຊ࣮૷ʢσϑΥϧτ࣮૷ྲྀ༻ʣ PI ຊ࣮૷ʢαϒΫϥεʹΑΔΦʔόʔϥΠυʣ PI ຊ࣮૷ʢσϑΥϧτ࣮૷ʣݺͼग़͠ PI ຊ࣮૷ʢΦʔόʔϥΠυࡁΈʣݺͼग़͠
  19. final ͡Όͳ͍ class ͸ɺ
 σϑΥϧτ࣮૷Λ࢖͏৔߹Ͱ΋ɺ
 PI ͷ࣮૷Λলུͯ͠͸ͳΒͳ͍ɻ Զ ݁࿦

  20. ⺄⇾獌K15&%ٛڋق⚸ק׿׻猳