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

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

主站蜘蛛池模板: 九江县| 陇南市| 山阳县| 德化县| 蒲江县| 洛扎县| 宜州市| 大田县| 娱乐| 皋兰县| 大兴区| 新蔡县| 通辽市| 宝兴县| 金湖县| 乌海市| 陈巴尔虎旗| 仪陇县| 夹江县| 吐鲁番市| 泰和县| 彩票| 元谋县| 恭城| 揭阳市| 南投县| 甘孜县| 丰宁| 宜兴市| 休宁县| 柞水县| 呼和浩特市| 韶关市| 吴忠市| 荥阳市| 额敏县| 东乌| 武安市| 栖霞市| 岳普湖县| 辽阳市|