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

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

主站蜘蛛池模板: 天水市| 全州县| 乌拉特后旗| 虎林市| 沐川县| 清徐县| 平潭县| 双牌县| 乌审旗| 宁武县| 库车县| 拉萨市| 千阳县| 沭阳县| 西宁市| 通海县| 花垣县| 乌恰县| 施秉县| 伊吾县| 洪江市| 安顺市| 察哈| 伊川县| 昔阳县| 定西市| 常德市| 永定县| 华安县| 安泽县| 澄迈县| 淳安县| 湖南省| 定州市| 洱源县| 潍坊市| 通山县| 读书| 牡丹江市| 长丰县| 太谷县|