Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
プロトコル指向フレンズ -Class to Struct-
Search
satoshin21
February 15, 2017
Programming
3
1.8k
プロトコル指向フレンズ -Class to Struct-
オブジェクト指向と対比して取り上げられるプロトコル指向について、ClassとStructそれぞれの特徴をベースにまとめました #potatotips37
satoshin21
February 15, 2017
Tweet
Share
More Decks by satoshin21
See All by satoshin21
少数精鋭で戦うための技術的改善について
satoshin21
3
1.2k
GTXiLibで小さく始めるAccessibility Testing
satoshin21
0
5k
iPhoneのカメラで写真撮影から現像までの技術を紐解く
satoshin21
4
3.3k
try! swift-sh
satoshin21
2
890
Reduxを取り入れて開発はpairs開発はどう変わったか
satoshin21
0
350
レガシーなアプリケーションの 60fps化を目指す為にやっていること
satoshin21
12
3.8k
Introducing CodeLayout with Tips
satoshin21
6
1.5k
World of No Interface Builder
satoshin21
0
1.8k
What I've done to attend WWDC
satoshin21
0
97
Other Decks in Programming
See All in Programming
知られざるDMMデータエンジニアの生態 〜かつてツチノコと呼ばれし者〜
takaha4k
1
450
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
240
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
6
700
Simple組み合わせ村から大都会Railsにやってきた俺は / Coming to Rails from the Simple
moznion
3
2.2k
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
300
[JAWS-UG横浜 #80] うわっ…今年のServerless アップデート、少なすぎ…?
maroon1st
0
100
ドメインイベント増えすぎ問題
h0r15h0
2
570
ErdMap: Thinking about a map for Rails applications
makicamel
1
660
ある日突然あなたが管理しているサーバーにDDoSが来たらどうなるでしょう?知ってるようで何も知らなかったDDoS攻撃と対策 #phpcon.2024
akase244
2
7.7k
Beyond ORM
77web
11
1.6k
Amazon Nova Reelの可能性
hideg
0
200
functionalなアプローチで動的要素を排除する
ryopeko
1
210
Featured
See All Featured
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
7
570
A Tale of Four Properties
chriscoyier
157
23k
GraphQLとの向き合い方2022年版
quramy
44
13k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.2k
Building Adaptive Systems
keathley
38
2.4k
Unsuck your backbone
ammeep
669
57k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
6
500
Building Better People: How to give real-time feedback that sticks.
wjessup
366
19k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Optimising Largest Contentful Paint
csswizardry
33
3k
Designing Experiences People Love
moore
139
23k
Transcript
ϓϩτίϧࢦϑϨϯζ - Class to Struct - @satoshin21
satoshin21 SatoshiN21 ࡔ ޛࢤ (Nagasaka Satoshi) - iOS Engineer of
pairs Div. eureka, Inc. - Swift, Objective-C, Java(Android), Apple Script
- Pairs - Couples - We are hiring!!
Objecive-C͔ΒSwift
ΦϒδΣΫτࢦ(OOP)͔Β ϓϩτίϧࢦ(POP)
ϓϩτίϧࢦͬͯʁ
protocolΛ͑ ϓϩτίϧࢦͰ͢
ͦΜͳ;͏ʹߟ͍͑ͯͨ࣌ظ͕ ʹ͋Γ·ͨ͠
POPͱOOP͕ܾఆతʹҧ͏ॴ
ClassΛ͏͔ʁ StructΛ͏͔ʁ
ͱߟ͑Δํ͕͍ۙ
͘͠
ΦϒδΣΫτࢦ(Class)ͷऑΛ
StructͱProtocolͰղܾ͢Δ ύϥμΠϜ
ͦΕ͕ϓϩτίϧࢦ
ΦϒδΣΫτࢦͱʁ
ΦϒδΣΫτࢦ ‣ &ODBQTVMBUJPO ΧϓηϧԽ ‣ "DDFTT$POUSPM Մࢹੑ੍ޚ ‣
"CTUSBDUJPO நԽ ‣ /BNFTQBDF ໊લۭؒ ‣ &YQSFTTJWF4ZOUBY දݱྗͷ͋ΔߏจɻϝιουϓϩύςΟΛ ܨ͛ͯॻ͚ͨΓͳͲ ‣ &YUFOTJCJMJUZ ֦ுੑ
ΦϒδΣΫτࢦ ‣ &ODBQTVMBUJPO ΧϓηϧԽ ‣ "DDFTT$POUSPM Մࢹੑ੍ޚ ‣
"CTUSBDUJPO நԽ ‣ /BNFTQBDF ໊લۭؒ ‣ &YQSFTTJWF4ZOUBY දݱྗͷ͋ΔߏจɻϝιουϓϩύςΟΛ ܨ͛ͯॻ͚ͨΓͳͲ ‣ &YUFOTJCJMJUZ ֦ுੑ ͜ΕΒܕ(Type)ͷಛ
ΦϒδΣΫτࢦͷऑ
ΦϒδΣΫτࢦͷऑ ‣ ҉తͳΦϒδΣΫτͷڞ༗ w ࢀর͍ͯ͠ΔΦϒδΣΫτ͕ଞͷΦϒδΣΫτͱڞ༗ w ʮࢀর͍ͯ͠ΔΦϒδΣΫτ͕উखʹҧ͏ͷʹͳͬͨΑʂͳʹ ͜Εͳʹ͜Εʂʯ
ΦϒδΣΫτࢦͷऑ ‣ ܧঝؔΛѲ͢Δͷ͕େม w ͜ͷ6TFS%FUBJM7JFX$POUSPMMFS%FUBJM7JFX$POUSPMMFSΛܧঝ ͍ͯͯ͠͞ΒʹͦͷεʔύʔΫϥεʹ#BTF7JFX$POUSPMMFS͕ w εʔύʔΫϥε͕ͲͷΑ͏ͳڍಈΛ͢Δͷ͔ɺͲͷΑ͏ͳΦϒδΣ ΫτΛอ͍࣋ͯ͠Δͷ͔ΛѲ͠ͳ͚ΕͳΒͳ͍ w
ʮ͜ͷεʔύʔΫϥεͷΛमਖ਼ͨ͠Βಈ͔ͳ͘ͳͬͨΑʔʂ ͢͝ʔ͍ʂʯ
ͳͲ
ͦ͜Ͱొ͢Δͷ͕ ϓϩτίϧࢦ
ϓϩτίϧࢦͰجຊతʹ ClassΘͳ͍
StructͱProtocolͷΈ
Class to Struct
ClassͱStruct $MBTT 4USVDU ܕ 5ZQF ࢀরܕ ܕ ܧঝ Ͱ͖Δ Ͱ͖ͳ͍
ϓϩύςΟมߋ Մೳ جຊతʹෆՄೳ
class DataStore { var strings = [String]() func add(string: String)
{ strings.append(string) } } ClassΛ༻͍ͨ߹
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Λ༻͍ͨ߹
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Λ༻͍ͨ߹
Structͷ߹
struct DataStore { var strings = [String]() mutating func add(string:
String) { strings.append(string) } } StructΛ༻͍ͨ߹
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Λ༻͍ͨ߹
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Λ༻͍ͨ߹
ࢀরઌ͕มԽ͢Δ͜ͱΛ ؾʹ͠ͳͯ͘ྑ͍
ͨͩɺStructܧঝ͕͑ͳ͍
is-aؔͱ͔ಉ͡;Δ·͍ఆٛ ͢Δͱ͖ͱ͔Ͳ͏͢Μͷʁ
ͦ͜ͰProtocol
ΦϒδΣΫτࢦʹΑΔܧঝؔ "OZ0CKFDU &NQMPZFF %FWFMPQFS %JSFDUPS J04%FWFMPQFS ʜ
ϓϩτίϧࢦ )BT1SPHSBNNJOH4LJMM %JSFDUPS J04%FWFMPQFS "OESPJE%FWFMPQFS )BT.BOBHFNFOU4LJMM
ϓϩτίϧࢦͷར ‣ ґଘؔΛϑϥοτʹͰ͖Δ ‣ ؔੑ͍͍͢ ‣ 1SPUPDPM&YUFOTJPOʹΑΓɺΦϒδΣΫτࢦͷܧঝͰ࣮ݱ͖ͯ͠ ͨίʔυͷ࠶ར༻ੑอͯΔ
Swift Standart LibraryͰɺ طʹclass1ͭΛ͢ͷΈ
͜ΕͰ໌͔Β ϓϩτίϧࢦϑϨϯζͩͶʔʂ
͙͢ʹͦ͠͏
ϓϩτίϧࢦͷ ‣ ΦʔϓϯιʔεԽ͞Ε͍ͯΔ͕ɺະͩ4XJGUJ04 ͳ͍͠ NBD04ΞϓϦ։ൃݴޠͷΠϝʔδ͕ڧ͍ ‣ ͭ·Γɺ$PDPB'SBNFXPSLͱྑ͘͠ͳ͚ΕͳΒͳ͍ ‣ $PDPB'SBNFXPSLDMBTTϕʔε ‣
ΦϒδΣΫτࢦͱ൱͕ԠͰ͖߹͏ඞཁ͕͋Δ ‣ ͦΕɺΦϒδΣΫτࢦͷةݥੑ ҉తΦϒδΣΫτڞ༗ Λ ΒΜͰ͍Δͱ͍͏͜ͱ
·ͨɺແཧʹ classͱstructΛڞଘͤ͞Δͱɾɾ
ClassͱStruct $MBTT 4USVDU ܕ 5ZQF ࢀরܕ ܕ ܧঝ Ͱ͖Δ Ͱ͖ͳ͍
ϓϩύςΟมߋ Մೳ جຊతʹෆՄೳ
ClassͱStruct $MBTT 4USVDU ܕ 5ZQF ࢀরܕ ܕ ܧঝ Ͱ͖Δ Ͱ͖ͳ͍
ϓϩύςΟมߋ Մೳ جຊతʹෆՄೳ ͜ͷҧ͍Λৗʹҙࣝ͢Δඞཁ͕͋Δ
·ͩզʑ ΦϒδΣΫτࢦ͔ΒΕΒΕͳ͍
SwiftϚϧνύϥμΠϜͳͷͰ ͏·͘ڞଘ͍ͤͯ͘͞ࣄ͕ॏཁ
ʔ͖ʔ͖ʂ
ύϥμΠϜʹΑͬͯ ͱ͍͘ͳ͜ͱҧ͏͔Βʂ
֤ύϥμΠϜͷಛੑΛཧղͯ͠ ॊೈͳϓϩάϥϛϯάΛ
Thank you IUUQTXXXOBTBHPWNJTTJPO@QBHFT/11OFXTFBSUIBUOJHIUIUNM