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.1k
GTXiLibで小さく始めるAccessibility Testing
satoshin21
0
4.9k
iPhoneのカメラで写真撮影から現像までの技術を紐解く
satoshin21
4
3.2k
try! swift-sh
satoshin21
2
880
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
95
Other Decks in Programming
See All in Programming
Keeping it Ruby: Why Your Product Needs a Ruby SDK - RubyWorld 2024
envek
0
180
[JAWS-UG横浜 #76] イケてるアップデートを宇宙いち早く紹介するよ!
maroon1st
0
460
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
4
210
今年のアップデートで振り返るCDKセキュリティのシフトレフト/2024-cdk-security-shift-left
tomoki10
0
190
nekko cloudにおけるProxmox VE利用事例
irumaru
3
420
テストコードのガイドライン 〜作成から運用まで〜
riku929hr
1
110
The Efficiency Paradox and How to Save Yourself and the World
hollycummins
1
440
テストケースの名前はどうつけるべきか?
orgachem
PRO
0
130
StarlingMonkeyを触ってみた話 - 2024冬
syumai
3
270
103 Early Hints
sugi_0000
1
220
htmxって知っていますか?次世代のHTML
hiro_ghap1
0
330
ブラウザ単体でmp4書き出すまで - muddy-web - 2024-12
yue4u
2
460
Featured
See All Featured
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
Building Your Own Lightsaber
phodgson
103
6.1k
The Cult of Friendly URLs
andyhume
78
6.1k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Designing Experiences People Love
moore
138
23k
Statistics for Hackers
jakevdp
796
220k
BBQ
matthewcrist
85
9.4k
Docker and Python
trallard
42
3.1k
Optimizing for Happiness
mojombo
376
70k
Thoughts on Productivity
jonyablonski
67
4.4k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
170
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