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

Unowned references

You can use unowned references when you can guarantee that the owner of another object will never exist when the reference is deallocated. Let's look at the following example:

class CreditCard {
let number: String
let expiry: String
unowned let owner: Person

init(owner: Person) {
self.owner = owner
self.number = "XXXXXXXXXXXXXXXX"
self.expiry = "XX/YY"
}
}

class Person {
let name: String
var cards: [CreditCard] = []

init(name: String) {
self.name = name
}
}

In this example, one person can have many credit cards. Each card needs to have an owner, which is immutable.

Let's look that how to use such an API:

let me = Person(name: "John Smith")
let card = CreditCard(owner: me)
let otherCard = CreditCard(owner: me)
me.cards = [card, otherCard]

In this example, me has two credit cards; each card has a back reference to its owner. In this particular case, we can always guarantee that the owner will never be deallocated before the cards. A CreditCard without an owner doesn't make any sense, and if we're trying to access the owner property of such a card, our program is probably not sane anymore. The behavior to notice here is that, compared to the weak modifier, the owner property on the CreditCard is not required to be an optional.

In this particular example, if we omitted the unowned qualifier, when there were no references left to Person, the back references from the cards would keep the object alive, effectively leaking both the credit cards and the owner of the cards.

Now that we've covered the semantics of memory ownership, we can dive deeply into the memory management and debugging tools that come with Xcode.

主站蜘蛛池模板: 澄江县| 聂拉木县| 博湖县| 米林县| 绩溪县| 抚远县| 许昌市| 留坝县| 措勤县| 镇坪县| 汕尾市| 蒙城县| 齐河县| 金溪县| 涿鹿县| 那曲县| 资源县| 岫岩| 邻水| 娱乐| 大埔区| 安塞县| 吉安市| 西盟| 梁山县| 天等县| 兖州市| 闻喜县| 南丰县| 峨眉山市| 吴桥县| 新建县| 永胜县| 武定县| 泰兴市| 烟台市| 重庆市| 旺苍县| 巴东县| 黑水县| 清涧县|