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

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

satoshin21
February 15, 2017

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

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

satoshin21

February 15, 2017
Tweet

More Decks by satoshin21

Other Decks in Programming

Transcript

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

    pairs Div. eureka, Inc. - Swift, Objective-C, Java(Android), Apple Script
  2. ΦϒδΣΫτࢦ޲ ‣ &ODBQTVMBUJPO ΧϓηϧԽ  ‣ "DDFTT$POUSPM Մࢹੑ੍ޚ  ‣

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

    "CTUSBDUJPO ந৅Խ  ‣ /BNFTQBDF ໊લۭؒ  ‣ &YQSFTTJWF4ZOUBY දݱྗͷ͋Δߏจɻϝιου΍ϓϩύςΟΛ ܨ͛ͯॻ͚ͨΓͳͲ  ‣ &YUFOTJCJMJUZ ֦ுੑ ͜ΕΒ͸ܕ(Type)ͷಛ௃
  4. class DataStore { var strings = [String]() func add(string: String)

    { strings.append(string) } } ClassΛ༻͍ͨ৔߹
  5. 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Λ༻͍ͨ৔߹
  6. 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Λ༻͍ͨ৔߹
  7. struct DataStore { var strings = [String]() mutating func add(string:

    String) { strings.append(string) } } StructΛ༻͍ͨ৔߹
  8. 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Λ༻͍ͨ৔߹
  9. 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Λ༻͍ͨ৔߹
  10. ClassͱStruct $MBTT 4USVDU ܕ 5ZQF ࢀরܕ ஋ܕ ܧঝ Ͱ͖Δ Ͱ͖ͳ͍

    ϓϩύςΟมߋ Մೳ جຊతʹෆՄೳ ͜ͷҧ͍Λৗʹҙࣝ͢Δඞཁ͕͋Δ