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
Swiftでなんで[weak self]するのか?
Search
oyuk
April 05, 2018
2
4.2k
Swiftでなんで[weak self]するのか?
oyuk
April 05, 2018
Tweet
Share
More Decks by oyuk
See All by oyuk
Shorebird について
oyuk
0
200
Material designのWindow size classについて
oyuk
0
740
LLVMについて調べた
oyuk
0
160
Swiftのmapからその次へ
oyuk
1
1.2k
Featured
See All Featured
Practical Orchestrator
shlominoach
183
9.7k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
660
120k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
33
6k
Happy Clients
brianwarren
92
6.4k
No one is an island. Learnings from fostering a developers community.
thoeni
16
2.1k
A better future with KSS
kneath
231
16k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
14
1.5k
jQuery: Nuts, Bolts and Bling
dougneiner
59
7.2k
Writing Fast Ruby
sferik
622
60k
What the flash - Photography Introduction
edds
64
11k
From Idea to $5000 a Month in 5 Months
shpigford
378
45k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
65
14k
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