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.3k
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Swiftでなんで[weak self]するのか?
oyuk
April 05, 2018
More Decks by oyuk
See All by oyuk
Shorebird について
oyuk
0
510
Material designのWindow size classについて
oyuk
0
1.5k
LLVMについて調べた
oyuk
0
220
Swiftのmapからその次へ
oyuk
1
1.4k
Featured
See All Featured
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
590
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
12k
Ethics towards AI in product and experience design
skipperchong
2
370
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Designing for Timeless Needs
cassininazir
1
470
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.3k
GitHub's CSS Performance
jonrohan
1033
470k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.4k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
410
Building Adaptive Systems
keathley
44
3.2k
A Modern Web Designer's Workflow
chriscoyier
699
190k
Skip the Path - Find Your Career Trail
mkilby
1
230
Transcript
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