- Hands-On Design Patterns with Swift
- Florent Vilmart Giordano Scalzo Sergio De Simone
- 139字
- 2021-07-02 14:45:05
Weak references
With weak references, you can safely store a reference to another object without increasing its retain count. This prevents creating cycles with all of the required safety. The weak references are always wrapped into Optionals and, unlike unowned references, when a weak reference gets deallocated, you can safely access it through the Optional interface and check that the object is still set and present at runtime:
class Task {
let description: String
weak var worker: Worker?
init(description: String) {
self.description = description
}
}
class Worker {
var name: String
var currentTask: Task?
init(name: String) {
self.name = name
}
}
let worker = Worker(name: "John Snow")
let task = Task(description: "Night's Watch Commander")
worker.currentTask = task
task.worker = worker
// John snow is the night watch's commander
worker.currentTask = nil
// the task will be deallocated
推薦閱讀
- 使用GitOps實現Kubernetes的持續部署:模式、流程及工具
- Creating Mobile Apps with Sencha Touch 2
- 分布式數據庫系統:大數據時代新型數據庫技術(第3版)
- Oracle RAC 11g實戰指南
- 大數據導論
- 網站數據庫技術
- 大數據精準挖掘
- 探索新型智庫發展之路:藍迪國際智庫報告·2015(上冊)
- 聯動Oracle:設計思想、架構實現與AWR報告
- Access 2010數據庫程序設計實踐教程
- 改進的群智能算法及其應用
- 數字化轉型實踐:構建云原生大數據平臺
- 碼上行動:利用Python與ChatGPT高效搞定Excel數據分析
- Mastering Java for Data Science
- 大數據用戶行為畫像分析實操指南