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

Using unowned

In our particular example, we cannot guarantee the duration of the life cycle of our objects, so unowned is unfit us example. Also, unowned provides fewer guarantees than weak, in terms of safety, can't be applied to Optionals. If you can't use weak for logic reasons, then there's little chance you'll be able to use unowned.

Let's investigate another piece of code involving credit cards, borrowed from Chapter 1, Refreshing the Basics.

First, let's see the code without the unowned modifier under the memory debugger:

class Card {
let owner: Person
init(_ owner: Person) {
self.owner = owner
}
}

class Person {
let name: String
var cards = [Card]()
init(name: String) {
self.name = name
}
}

func runTests() {
let batman = Person(name: "Batman")
batman.cards.append(Card(batman))
batman.cards.append(Card(batman))
batman.cards.append(Card(batman))
}

runTests()

This simple program should not leave any objects behind after runTests() finishes running; however, because we have a strong reference cycle between Person and Card, that is not the case, and all of the created objects will leak:

All of our objects have leaked, as shown in the preceding screenshot. This example is perfect for using unowned. The Card object can't live without an owner. So, whenever the owner is deallocated, all of the cards should be destroyed as well, as we'll never reference a card without its owner. We need to update our Card class to reflect that the Card objects are not retaining their owner:

class Card {
unowned let owner: Person
init(_ owner: Person) {
self.owner = owner
}
}

With this addition, the card cannot be allocated without a Person, and this person has to exist to be a valid card. In the next section, you'll see the issues that unowned can cause, and how we can prevent them.

主站蜘蛛池模板: 阿拉善左旗| 微山县| 五指山市| 博白县| 芷江| 成武县| 定边县| 格尔木市| 色达县| 卓资县| 北流市| 石河子市| 麻栗坡县| 定日县| 石阡县| 罗江县| 大同县| 罗江县| 井陉县| 沂源县| 莱西市| 南澳县| 黎平县| 霍邱县| 福州市| 兴安县| 灵石县| 永嘉县| 五原县| 南部县| 资源县| 平江县| 丰顺县| 广丰县| 辽中县| 和硕县| 焉耆| 青田县| 杨浦区| 嘉义市| 江山市|