- 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
推薦閱讀
- 計(jì)算機(jī)綜合設(shè)計(jì)實(shí)驗(yàn)指導(dǎo)
- 數(shù)據(jù)庫(kù)原理及應(yīng)用教程(第4版)(微課版)
- 云計(jì)算環(huán)境下的信息資源集成與服務(wù)
- InfluxDB原理與實(shí)戰(zhàn)
- 數(shù)據(jù)要素五論:信息、權(quán)屬、價(jià)值、安全、交易
- Sybase數(shù)據(jù)庫(kù)在UNIX、Windows上的實(shí)施和管理
- Microsoft Power BI數(shù)據(jù)可視化與數(shù)據(jù)分析
- Python金融實(shí)戰(zhàn)
- 數(shù)據(jù)科學(xué)工程實(shí)踐:用戶行為分析與建模、A/B實(shí)驗(yàn)、SQLFlow
- 云數(shù)據(jù)中心網(wǎng)絡(luò)與SDN:技術(shù)架構(gòu)與實(shí)現(xiàn)
- 改進(jìn)的群智能算法及其應(yīng)用
- 數(shù)據(jù)指標(biāo)體系:構(gòu)建方法與應(yīng)用實(shí)踐
- SQL Server 2008寶典(第2版)
- ORACLE 11g權(quán)威指南
- Python金融數(shù)據(jù)挖掘與分析實(shí)戰(zhàn)