Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Objective-Cのpropertyのこと
Search
Toru Furuya
February 20, 2017
60
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Objective-Cのpropertyのこと
Toru Furuya
February 20, 2017
More Decks by Toru Furuya
See All by Toru Furuya
How To Built Uranow Android App
torufuruya
0
73
Automatic Kotlin Lint with Pipeline and Danger
torufuruya
0
110
Realm Academyのすすめ
torufuruya
0
50
iPhone XでのSafe AreaとWebコンテンツの表示制限について
torufuruya
0
110
Git subtree の導入
torufuruya
0
120
CI as Code の導入
torufuruya
0
54
Getting Started Yeoman
torufuruya
0
50
Featured
See All Featured
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.6k
Facilitating Awesome Meetings
lara
57
7.2k
How to Ace a Technical Interview
jacobian
280
24k
Fireside Chat
paigeccino
43
4.1k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Site-Speed That Sticks
csswizardry
13
1.6k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
1
450
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.9k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
750
Agile that works and the tools we love
rasmusluckow
331
22k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2.2k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
600
Transcript
Objective-Cのpropertyのこと 2017.02.20 Toru Furuya
まず言っておくと • ググればすぐたくさん情報出てくるし分かりやすい • けどなんか皆いろいろな書き方しててまとまりがない • とどのつまり公式ドキュメント読もう • 今日の話もいろんなサイトからキュレーションしてきた •
おかしなところは突っ込んでください
1. プロパティとインスタンス変数の違い 2. プロパティ属性の種類
1. プロパティとインスタンス変数 の違い
“@ で始まるものはプログラムでは無く、コンパイラへの指示となります。この様な指示を Objective-C ではコンパイラディレクティブ (Compiler directive) と呼びます。 ” http://d.hatena.ne.jp/nakamura001/20101101/1288632739
プロパティ=インスタンス変数を作るための指令
// MyClass.h @interface MyClass : ParentClass @property (nonatomic, readonly, copy)
NSString *myProp; // プロパティ @end // MyClass.m @implementation MyClass { NSString *_myIvar; // インスタンス変数 // 暗黙的に定義されてる(と思う) //NSString *_myProp; // インスタンス変数以外にも setter,getter とか } @end
2. プロパティ属性の種類
ARCの環境 • アトミック性属性 ◦ atomic (default) ◦ nonatomic • 所有属性
◦ strong (default) ◦ weak ◦ copy ◦ (assign) • アクセス制御属性 ◦ readwrite (default) ◦ readonly
アトミック性属性 atomic : Read と同時に別スレッドで Write が走っても Write 前後どちらかの値が取得できる ことは保証される(その仕組みのために
nonatomic より処理は遅いから頻繁に参照される 値には使わない方が良い)。排他処理だがスレッドセーフではない。 nonatomic : Read と同時に別スレッドで Write が走ると Read 元に不完全なデータが返る可 能性がある。
// MyClass.h @interface MyClass : ParentClass @property (atomic) NSString *myProp;
// プロパティ @end // MyClass.m @implementation MyClass - (void)someFunction { // インスタンス変数に直接代入→ atomic の排他処理にならない _myProp = @“hoge”; //setter 経由で代入→ atomic の排他処理になる self.myProp = @“fuga”; } @end
排他処理だがスレッドセーフではない ??? “ 自動生成される getter と setter はそれぞれの中で排他処理が行われますが、 getter と
setter をまたがって排他処理が行われるわけではないのです。 ” “ そのため、マルチスレッド環境ではタイミングによっては setter が呼び出される前に別の スレッドからも getter で値が参照されるという事態が起こり、不正な結果となるわけです。 ” http://ameblo.jp/tomotaken/entry-10818354164.html
スレッドセーフを実現するには Objective-C でのスレッドセーフの実装方法だけで 1 回勉強会できてしまいそうなので今回 は割愛(自分が読んでないだけ mm ) • @synchronized
による排他制御を行う • ミューテックスによる排他制御を行う • セマフォによる排他制御を行う • NSLock による排他制御を行う • NSRecursiveLock による排他制御を行う http://program.station.ez-net.jp/special/objective-c/atomic.asp
所有属性 strong : オーナーシップを持つ(参照カウントがincrementされる) weak : オーナーシップを持たない(参照カウントがincrementされない)。オブジェクトが解放 されるとnilを返す様になる。 copy :
コピーに対してオーナーシップを持つ(setterに渡されたオブジェクトのコピーを作る ため、元のオブジェクトとは違うオブジェクトを持つ) わかりやすいブログ
weak - 循環参照を防止する Parent Child @property (strong) Child *child child.parent
= self; @property (strong) Parent *parent 解放したい 解放したい childが参照 してるから無理 parentが参照 してるから無理
Parent Child @property (strong) Child *child child.parent = self; @property
(weak) Parent *parent 解放したい 解放したい OK!!! parentが参照 してるから無理
copy - サンプル // SomeModel.h @interface SomeModel : NSObject @property
(nonatomic) NSString *string; @property (nonatomic, copy) NSString *copiedString; @end // MyClass.m @implementation MyClass - (void)someFunction { SomeModel *model = [[SomeModel alloc] init]; NSMutableString *baseString = [NSMutableString stringWithString:@"111"]; model.string = baseString; model.copiedString = baseString; [baseString appendString:@"!!!"]; NSLog(@"string : %@", con.string); //結果:111!!! NSLog(@"copied string : %@", con.copiedString); //結果:111 } @end
copy - Googleのobjective-cコーディングガイド “NSString properties should always be declared with
the copy attribute.” “This logically follows from the requirement that setters for NSStrings always must use copy instead of retain.” https://google.github.io/styleguide/objcguide.xml?showone=Properties#Properties
アクセス制御属性 readwrite と readonly 特定のインスタンス変数を外部には readonly, 実装クラスからは readwrite にするとかもで きる
// MyClass.h @interface MyClass : ParentClass @property (readonly) NSString *myProp; @end // MyClass.m @interface MyClass() @property (readwrite) NSString *myProp; @end
まとめ • nonatomic 指定してないところは見直そう! • 循環参照に気をつけよう! • readonly にできるものはしよう!