官术网_书友最值得收藏!

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

主站蜘蛛池模板: 长泰县| 益阳市| 左贡县| 宾阳县| 新和县| 建宁县| 金沙县| 呼伦贝尔市| 阿拉善左旗| 遂川县| 旺苍县| 弥渡县| 杂多县| 肃南| 凤山市| 仙桃市| 渭南市| 芜湖市| 黎平县| 宜兰县| 东辽县| 宜都市| 余庆县| 洛隆县| 同江市| 土默特右旗| 科技| 南开区| 和林格尔县| 定安县| 五河县| 龙泉市| 新邵县| 稻城县| 栾城县| 西充县| 眉山市| 彭阳县| 甘泉县| 大庆市| 太和县|