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
Swiftでなんで[weak self]するのか?
Search
oyuk
April 05, 2018
5.4k
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Swiftでなんで[weak self]するのか?
oyuk
April 05, 2018
Transcript
19 slides
Swiftでなんで[weak self]するのか? 2018/03/30 oyuk(@oydku) 1
自己紹介 oyuk(@oydku) プログラマ 2
weak selfについて よくあるアレ { [weak self] event in guard let
strongSelf = self else { return } // なんかする } 3
結論から [weak self] と書くことでクロージャが self を弱参照し、クロージャと self の循環参照を防ぐ 4
は? [weak self] ってなんじゃその書き方は 弱参照?循環参照? 5
[weak self] ってそんな書き方ありなん? 今回の本質じゃないから軽く 「 [weak self] と書くことでクロージャが self を弱参照する」これだけ覚えておいて
ちなみに [weak self] はキャプチャリストと呼ぶ 6
弱参照?循環参照? 参照カウントというGCの概念 これを理解するために 参照カウントについて Swiftの参照カウント(Automatic Reference Counting)について少し 7
参照カウント GCの一種 オブジェクトに対して参照している他のオブジェクトの数を記憶する(参照カウント) 1以上なら生きている。メモリに割り当てられている 0になると死。解放される 8
参照カウント 例1 class Hoge {} // Hoge のインスタンスの参照カウントが1 になる var
h: Hoge? = Hoge() 9
参照カウント 例2 class Hoge {} // Hoge のインスタンスの参照カウントが1 になる var
h: Hoge? = Hoge() // Hoge のインスタンスの参照カウントが0 になり解放される h = nil 10
参照カウント 例3 class Hoge { var fuga: Fuga? } class
Fuga { var hoge: Hoge? } var hoge: Hoge? = Hoge() // hoge の参照カウント1 var fuga: Fuga? = Fuga() // fuga の参照カウント1 hoge.fuga = fuga // hoge の参照カウント2 fuga.hoge = hoge // fuga の参照カウント2 hoge = nil // hoge の参照カウント1 fuga = nil // fuga の参照カウント1 hoge も fuga も参照することができないのにメモリ上に残ったままになっている hoge と fugga で循環参照が起きている 11
参照カウント 例4 class Hoge { private var closure: (() ->
Void)? private var count = 0 init() { closure = createClosure() } func createClosure() -> (()-> Void) { return { self.count += 1 } } } var h: Hoge? = Hoge() // Hoge の参照カウント2 , closure の参照カウント1 h = nil // Hoge の参照カウント1 hoge と closure で循環参照が起きている 12
循環参照 var h: Hoge? = Hoge() // Hoge の参照カウント2 ,
closure の参照カウント1 h = nil // Hoge の参照カウント1 13
循環参照解決策 class Hoge { private var closure: (() -> Void)?
private var count = 0 init() { closure = createClosure() } func createClosure() -> (()-> Void) { return { [weak self] in self?.count += 1 } } } var h: Hoge? = Hoge() // Hoge の参照カウント1 , closure の参照カウント1 h = nil // Hoge の参照カウント0 解放される! 14
なぜ func createClosure() -> (()-> Void) { return { [weak
self] in self?.count += 1 } } [weak self] は self を参照するが参照カウントは+1しないという意味 これが弱参照 ちなみに今までのは強参照 循環参照が起こらなくなる 15
なぜ var h: Hoge? = Hoge() // Hoge の参照カウント1 ,
closure の参照カウント1 h = nil // Hoge の参照カウント0 16
なぜ hは参照カウントは0なので解放される closureも参照カウント0なので解放される 17
まとめ { [weak self] event in guard let strongSelf =
self else { return } // なんかする } [weak self] と書くことでクロージャが self を弱参照し、クロージャと self の循環参照を 防ぐ 18
19
More Decks by oyuk
See All by oyuk
Shorebird について
oyuk
0
530
Material designのWindow size classについて
oyuk
0
1.6k
LLVMについて調べた
oyuk
0
230
Swiftのmapからその次へ
oyuk
1
1.4k
Featured
See All Featured
Amusing Abliteration
ianozsvald
1
390
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
550
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
The Cost Of JavaScript in 2023
addyosmani
56
10k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
330
Designing for Performance
lara
612
70k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.4k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.4k
エンジニアに許された特別な時間の終わり
watany
109
250k
GraphQLとの向き合い方2022年版
quramy
50
15k
Chasing Engaging Ingredients in Design
codingconduct
0
390
A Tale of Four Properties
chriscoyier
163
24k