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

SwiftにおけるMethod Dispatchについて

SwiftにおけるMethod Dispatchについて

SwiftのMethod Dispatchについて色々とまとめてみました。

1516d3136f143834c8af104b68468aa6?s=128

Naohiro Hamada

February 14, 2017
Tweet

Transcript

  1. METHOD DISPATCH@SWIFT SWIFTѪ޷ձ VOL.16 N.HAMADA

  2. ࣗݾ঺հ w J04ྺ೥͘Β͍ w ੲ͸ɺNBD04ΞϓϦͷ։ൃΛ΍͍ͬͯ·ͨ͠ w 2JJUB w IUUQRJJUBDPN)B/P)JUP w

    (JU)VC w IUUQTHJUIVCDPNOIBNBEB 
  3. ࠓ೔ͷ͓࿩ w .FUIPE%JTQBUDIʹ͍ͭͯ w 4XJGUʹ͓͚Δ.FUIPE%JTQBUDIͷ͍Ζ͍Ζ 

  4. ໰୊ protocol AlcoholProtocol { } extension AlcoholProtocol { func alcohol()

    -> String { return "" } } class TwoBeers { } extension TwoBeers: AlcoholProtocol { func alcohol() -> String { return "" } } let beerServer: AlcoholProtocol = TwoBeers() let myBeer = beerServer.alcohol() print(myBeer) 
  5. ໰୊ protocol AlcoholProtocol { } extension AlcoholProtocol { func alcohol()

    -> String { return "" } } class TwoBeers { } extension TwoBeers: AlcoholProtocol { func alcohol() -> String { return "" } } let beerServer: AlcoholProtocol = TwoBeers() let myBeer = beerServer.alcohol() print(myBeer) print(myBeer)Ͱग़ྗ͞ΕΔͷ͸ ͱͷͲͬͪʁ 
  6. .FUIPE%JTQBUDIͱ͸ʁ w ιʔείʔυ্ͷʮϝιουݺͼग़͠ʯͰɺ࣮ࡍʹ ݺͼग़͞ΕΔϝιου͸ɺͲͷϝιουͳͷ͔ʁɹ ΛܾΊΔػߏ extension AlcoholProtocol { func alcohol()

    -> String { return "" } } class TwoBeers { } extension TwoBeers: AlcoholProtocol { func alcohol() -> String { return "" } } let beerServer: AlcoholProtocol = TwoBeers() let myBeer = beerServer.alcohol() ʁ 
  7. .FUIPE%JTQBUDIͷछྨ 4UBUJD ௚઀తʹɺϝιουͷݺͼग़͠Λߦ͏ ίϯύΠϧ࣌ 75BCMF Ծ૝ςʔϒϧΛ࢖ͬͯݺͼग़͢ϝιουΛ ܾΊΔ ࣮ߦ࣌ .FTTBHF ϝοηʔδΛૹΔ͜ͱͰɺͲͷϝιουΛ

    ݺͿ͔ܾΊΔ ࣮ߦ࣌ 8JUOFTT ࢀরςʔϒϧΛ࢖ͬͯݺͼग़͢ϝιουΛ ܾΊΔ ࣮ߦ࣌ 
  8. 4UBUJD%JTQBUDI w ίϯύΠϧ࣌ʹɺͲͷϝιο υΛݺͼग़͔͢ɺ͕ܾ·Δ TUSVDU#FFS4FSWFS\ GVODCFFS 4USJOH\ SFUVSO ^ GVODCFFST

    4USJOH\ SFUVSO ^ ^ DMBTT#FFS4FSWFS\ pOBMGVODCFFS 4USJOH\ SFUVSO ^ ^ 
  9. 75BCMF w Ծ૝ςʔϒϧΛ࢖ͬͯɺ ࣮ߦ࣌ʹݺͼग़͢ϝιο υΛܾΊΔ w Ϋϥε͸ɺԾ૝ςʔϒ ϧΛ࣋ͭ class AlcoholServer

    { func alcohol() -> String { return "" } func cheers() -> String { return "" } } class SakeMaker: AlcoholServer { override func alcohol() -> String { return "" } } 
  10. 75BCMF class AlcoholServer { func alcohol() -> String { return

    "" } func cheers() -> String { return "" } } class SakeMaker: AlcoholServer { override func alcohol() -> String { return "" } } "MDPIPM4FSWFS BMDPIPM "MDPIPM4FSWFS DIFFST "MDPIPM4FSWFS 4BLF.BLFS BMDPIPM 4BLF.BLFS DIFFST "MDPIPM4FSWFS 
  11. .FTTBHF w .FTTBHFΛ࢖ͬͯɺͲͷ ϝιουΛݺͼग़͔͢Λ ܾΊΔ w 0CKFDUJWF$Ͱͷϝιου ݺͼग़͠ w 'PVOEBUJPOΛΠϯϙʔ

    τ͢Δඞཁ͕͋Δ JNQPSU'PVOEBUJPO DMBTT#FFS4FSWFS\ EZOBNJDGVODBMDPIPM 4USJOH\ SFUVSO ^ EZ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ODBMDPIPM 4USJOH\ SFUVSO ^ ^ FYUFOTJPO#FFS4FSWFS\ GVODDIFFST 4USJOH\

    SFUVSO ^ ^ w .FTTBHF w 8JUOFTT w 4UBUJD 
  19. ໰୊ protocol AlcoholProtocol { } extension AlcoholProtocol { func alcohol()

    -> String { return "" } } class TwoBeers { } extension TwoBeers: AlcoholProtocol { func alcohol() -> String { return "" } } let beerServer: AlcoholProtocol = TwoBeers() let myBeer = beerServer.alcohol() print(myBeer) print(myBeer)Ͱग़ྗ͞ΕΔͷ͸ ͱͷͲͬͪʁ
  20. ໰୊ protocol AlcoholProtocol { } extension AlcoholProtocol { func alcohol()

    -> String { return "" } } class TwoBeers { } extension TwoBeers: AlcoholProtocol { func alcohol() -> String { return "" } } let beerServer: AlcoholProtocol = TwoBeers() let myBeer = beerServer.alcohol() print(myBeer)
  21. ·ͱΊ w 4XJGUͰͷ.FUIPE%JTQBUDIΛ؆୯ʹ·ͱΊͯΈ· ͨ͠