- Hands-On Design Patterns with Swift
- Florent Vilmart Giordano Scalzo Sergio De Simone
- 223字
- 2021-07-02 14:44:59
Using weak and unowned
Swift provides us with two keywords that indicate how we want to extend the lifetime of an object in a closure. While both prevent creating retain cycles, they are fundamentally different.
Using weak will wrap the captured value inside of an optional, indicating that the instance may have been deallocated before the closure was executed:
class MyClass {
var running = false
func run() {
running = true
DispatchQueue.main.asyncAfter(deadline: .now() + 10) { [weak self] in
self?.running = false
}
}
}
var instance: MyClass? = MyClass()
instance?.run()
instance = nil
In this execution, instance will immediately be deallocated when set to nil.
Using unowned indicates that the variable won't be owned by the block. Another mechanism should be responsible for ensuring that the lifetime of the captured object is properly extended until the block is executed:
class MyClass {
var running = false
func run() {
running = true
DispatchQueue.main.asyncAfter(deadline: .now() + 10) { [unowned self] in
self.running = false
}
}
}
var instance: MyClass? = MyClass()
instance?.run()
instance = nil
In this case, your program will crash when the block is executing, because the self variable will be deallocated upon the execution of the block:
Fatal error: Attempted to read an unowned reference but object 0x7f80bc75a4e0 was already deallocated
- Word 2010中文版完全自學手冊
- 從0到1:數據分析師養成寶典
- Libgdx Cross/platform Game Development Cookbook
- 揭秘云計算與大數據
- Ceph源碼分析
- Hands-On Mathematics for Deep Learning
- 基于OPAC日志的高校圖書館用戶信息需求與檢索行為研究
- 網站數據庫技術
- MATLAB Graphics and Data Visualization Cookbook
- Oracle PL/SQL實例精解(原書第5版)
- IPython Interactive Computing and Visualization Cookbook(Second Edition)
- Python數據分析與數據化運營
- Chef Essentials
- 企業主數據管理實務
- 數據庫應用系統技術