December 2018
Intermediate to advanced
414 pages
10h 19m
English
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: ...
Read now
Unlock full access