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

プロトコル指向フレンズ -Class to Struct-

F05557f473bac91b5bdc6c2e8a95c6f2?s=47 satoshin21
February 15, 2017

プロトコル指向フレンズ -Class to Struct-

オブジェクト指向と対比して取り上げられるプロトコル指向について、ClassとStructそれぞれの特徴をベースにまとめました #potatotips37

F05557f473bac91b5bdc6c2e8a95c6f2?s=128

satoshin21

February 15, 2017
Tweet

Transcript

  1. ϓϩτίϧࢦ޲ϑϨϯζ - Class to Struct - @satoshin21

  2. satoshin21 SatoshiN21 ௕ࡔ ޛࢤ (Nagasaka Satoshi) - iOS Engineer of

    pairs Div. eureka, Inc. - Swift, Objective-C, Java(Android), Apple Script
  3. - Pairs - Couples - We are hiring!!

  4. Objecive-C͔ΒSwift΁

  5. ΦϒδΣΫτࢦ޲(OOP)͔Β ϓϩτίϧࢦ޲(POP)΁

  6. ϓϩτίϧࢦ޲ͬͯʁ

  7. protocolΛ࢖͑͹ ϓϩτίϧࢦ޲Ͱ͢

  8. ͦΜͳ;͏ʹߟ͍͑ͯͨ࣌ظ͕ ๻ʹ΋͋Γ·ͨ͠

  9. POPͱOOP͕ܾఆతʹҧ͏ॴ͸

  10. ClassΛ࢖͏͔ʁ StructΛ࢖͏͔ʁ

  11. ͱߟ͑Δํ͕͍ۙ

  12. ΋͘͠͸

  13. ΦϒδΣΫτࢦ޲(Class)ͷऑ఺Λ

  14. StructͱProtocolͰղܾ͢Δ ύϥμΠϜ

  15. ͦΕ͕ϓϩτίϧࢦ޲

  16. ΦϒδΣΫτࢦ޲ͱ͸ʁ

  17. ΦϒδΣΫτࢦ޲ ‣ &ODBQTVMBUJPO ΧϓηϧԽ  ‣ "DDFTT$POUSPM Մࢹੑ੍ޚ  ‣

    "CTUSBDUJPO ந৅Խ  ‣ /BNFTQBDF ໊લۭؒ  ‣ &YQSFTTJWF4ZOUBY දݱྗͷ͋Δߏจɻϝιου΍ϓϩύςΟΛ ܨ͛ͯॻ͚ͨΓͳͲ  ‣ &YUFOTJCJMJUZ ֦ுੑ
  18. ΦϒδΣΫτࢦ޲ ‣ &ODBQTVMBUJPO ΧϓηϧԽ  ‣ "DDFTT$POUSPM Մࢹੑ੍ޚ  ‣

    "CTUSBDUJPO ந৅Խ  ‣ /BNFTQBDF ໊લۭؒ  ‣ &YQSFTTJWF4ZOUBY දݱྗͷ͋Δߏจɻϝιου΍ϓϩύςΟΛ ܨ͛ͯॻ͚ͨΓͳͲ  ‣ &YUFOTJCJMJUZ ֦ுੑ ͜ΕΒ͸ܕ(Type)ͷಛ௃
  19. ΦϒδΣΫτࢦ޲ͷऑ఺

  20. ΦϒδΣΫτࢦ޲ͷऑ఺ ‣ ҉໧తͳΦϒδΣΫτͷڞ༗ w ࢀর͍ͯ͠ΔΦϒδΣΫτ͕ଞͷΦϒδΣΫτͱڞ༗ w ʮࢀর͍ͯ͠ΔΦϒδΣΫτ͕উखʹҧ͏΋ͷʹͳͬͨΑʂͳʹ ͜Εͳʹ͜Εʂʯ

  21. ΦϒδΣΫτࢦ޲ͷऑ఺ ‣ ܧঝؔ܎Λ೺Ѳ͢Δͷ͕େม w ͜ͷ6TFS%FUBJM7JFX$POUSPMMFS͸%FUBJM7JFX$POUSPMMFSΛܧঝ ͍ͯͯ͠͞ΒʹͦͷεʔύʔΫϥεʹ͸#BTF7JFX$POUSPMMFS͕ w εʔύʔΫϥε͕ͲͷΑ͏ͳڍಈΛ͢Δͷ͔ɺͲͷΑ͏ͳΦϒδΣ ΫτΛอ͍࣋ͯ͠Δͷ͔Λ೺Ѳ͠ͳ͚Ε͹ͳΒͳ͍ w

    ʮ͜ͷεʔύʔΫϥεͷ஋Λमਖ਼ͨ͠Βಈ͔ͳ͘ͳͬͨΑʔʂ    ͢͝ʔ͍ʂʯ
  22. ͳͲ

  23. ͦ͜Ͱొ৔͢Δͷ͕ ϓϩτίϧࢦ޲

  24. ϓϩτίϧࢦ޲Ͱ͸جຊతʹ Class͸࢖Θͳ͍

  25. StructͱProtocolͷΈ

  26. Class to Struct

  27. ClassͱStruct $MBTT 4USVDU ܕ 5ZQF ࢀরܕ ஋ܕ ܧঝ Ͱ͖Δ Ͱ͖ͳ͍

    ϓϩύςΟมߋ Մೳ جຊతʹෆՄೳ
  28. class DataStore { var strings = [String]() func add(string: String)

    { strings.append(string) } } ClassΛ༻͍ͨ৔߹
  29. let dataStore = DataStore() dataStore.add(string: "Foo") dataStore.add(string: "Bar") let classA

    = ReferenceClassA(dataStore: dataStore) classA.add(string: "hoge") let stringA = classA.dataStoreString() // -> [“Foo","Bar","hoge"] ClassΛ༻͍ͨ৔߹
  30. let dataStore = DataStore() dataStore.add(string: "Foo") dataStore.add(string: “Bar") let classA

    = ReferenceClassA(dataStore: dataStore) classA.add(string: "hoge") let stringA = classA.dataStoreString() // -> ["Foo","Bar","hoge"] let classB = ReferenceClassB(dataStore: dataStore) classB.dataStore.strings.removeAll() let stringAre = classA.dataStoreString() // -> [] ClassΛ༻͍ͨ৔߹
  31. Structͷ৔߹

  32. struct DataStore { var strings = [String]() mutating func add(string:

    String) { strings.append(string) } } StructΛ༻͍ͨ৔߹
  33. var dataStore = DataStore() dataStore.add(string: "Foo") dataStore.add(string: "Bar") var structA

    = ReferenceStructA(dataStore: dataStore) structA.dataStore.add(string: "hoge") let stringA = structA.dataStoreString() // -> [“Foo","Bar","hoge"] StructΛ༻͍ͨ৔߹
  34. var dataStore = DataStore() dataStore.add(string: "Foo") dataStore.add(string: "Bar") var structA

    = ReferenceStructA(dataStore: dataStore) structA.dataStore.add(string: "hoge") let stringA = structA.dataStoreString() // -> ["Foo","Bar","hoge"] var structB = ReferenceStructB(dataStore: dataStore) structB.dataStore.strings.removeAll() let stringAre = structA.dataStoreString() // -> ["Foo","Bar","hoge"] StructΛ༻͍ͨ৔߹
  35. ࢀরઌ͕มԽ͢Δ͜ͱΛ ؾʹ͠ͳͯ͘ྑ͍

  36. ͨͩɺStruct͸ܧঝ͕࢖͑ͳ͍

  37. is-aؔ܎ͱ͔ಉ͡;Δ·͍ఆٛ ͢Δͱ͖ͱ͔Ͳ͏͢Μͷʁ

  38. ͦ͜ͰProtocol

  39. ΦϒδΣΫτࢦ޲ʹΑΔܧঝؔ܎ "OZ0CKFDU &NQMPZFF %FWFMPQFS %JSFDUPS J04%FWFMPQFS ʜ

  40. ϓϩτίϧࢦ޲ )BT1SPHSBNNJOH4LJMM %JSFDUPS J04%FWFMPQFS "OESPJE%FWFMPQFS )BT.BOBHFNFOU4LJMM

  41. ϓϩτίϧࢦ޲ͷར఺ ‣ ґଘؔ܎ΛϑϥοτʹͰ͖Δ ‣ ؔ܎ੑ΋௥͍΍͍͢ ‣ 1SPUPDPM&YUFOTJPOʹΑΓɺΦϒδΣΫτࢦ޲ͷܧঝͰ࣮ݱ͖ͯ͠ ͨίʔυͷ࠶ར༻ੑ΋อͯΔ

  42. Swift Standart LibraryͰ͸ɺ طʹclass͸1ͭΛ࢒͢ͷΈ

  43. ͜ΕͰ໌೔͔Β ϓϩτίϧࢦ޲ϑϨϯζͩͶʔʂ

  44. ͙͢ʹ͸೉ͦ͠͏

  45. ϓϩτίϧࢦ޲ͷ໰୊఺ ‣ ΦʔϓϯιʔεԽ͞Εͯ͸͍Δ͕ɺະͩ4XJGU͸J04 ͳ͍͠͸ NBD04ΞϓϦ։ൃݴޠͷΠϝʔδ͕ڧ͍ ‣ ͭ·Γɺ$PDPB'SBNFXPSLͱ஥ྑ͘͠ͳ͚Ε͹ͳΒͳ͍ ‣ $PDPB'SBNFXPSL͸DMBTTϕʔε ‣

    ΦϒδΣΫτࢦ޲ͱ൱͕ԠͰ΋෇͖߹͏ඞཁ͕͋Δ ‣ ͦΕ͸ɺΦϒδΣΫτࢦ޲ͷةݥੑ ҉໧తΦϒδΣΫτڞ༗ Λ͸ ΒΜͰ͍Δͱ͍͏͜ͱ
  46. ·ͨɺແཧʹ classͱstructΛڞଘͤ͞Δͱɾɾ

  47. ClassͱStruct $MBTT 4USVDU ܕ 5ZQF ࢀরܕ ஋ܕ ܧঝ Ͱ͖Δ Ͱ͖ͳ͍

    ϓϩύςΟมߋ Մೳ جຊతʹෆՄೳ
  48. ClassͱStruct $MBTT 4USVDU ܕ 5ZQF ࢀরܕ ஋ܕ ܧঝ Ͱ͖Δ Ͱ͖ͳ͍

    ϓϩύςΟมߋ Մೳ جຊతʹෆՄೳ ͜ͷҧ͍Λৗʹҙࣝ͢Δඞཁ͕͋Δ
  49. ·ͩզʑ͸ ΦϒδΣΫτࢦ޲͔Β཭ΕΒΕͳ͍

  50. Swift͸ϚϧνύϥμΠϜͳͷͰ ͏·͘ڞଘ͍ͤͯ͘͞ࣄ͕ॏཁ

  51. ΁ʔ͖΁ʔ͖ʂ

  52. ύϥμΠϜʹΑͬͯ ͱ͍͘ͳ͜ͱҧ͏͔Βʂ

  53. ֤ύϥμΠϜͷಛੑΛཧղͯ͠ ॊೈͳϓϩάϥϛϯάΛ

  54. Thank you IUUQTXXXOBTBHPWNJTTJPO@QBHFT/11OFXTFBSUIBUOJHIUIUNM