I was reading the swift programming guide, and in the initializers chapters I came across this code:
class Customer {
let name: String
var card: CreditCard?
init(name: String) {
self.name = name
}
deinit { println("\(name) is being deinitialized") }
}
class CreditCard {
let number: Int
unowned let customer: Customer
init(number: Int, customer: Customer) {
self.number = number
self.customer = customer
}
deinit { println("Card #\(number) is being deinitialized") }
}
So I tried to try the code myself and make some changes to see what happens (makes me understand more )
So I opened the playground and started typing, after that I noticed that the weak
keyword and the unowned
keyword aren't recognized by the playground. This could only mean that playground only support strong reference variables.
So why does playground only support strong reference, and what could you do to prevent a strong reference cycle in playground?
The playground is only a playground. It isn't a very exact representative of real life. So you should not be surprised if it treated memory management differently from real life. (Another example: the top level of a playground is clearly not like the top level of a real Swift file, since you can put things there that are not declarations and they work.)
You can use a playground for developing algorithms interactively, but only real life (i.e. a compilable executable .swift file) is real life.
For example, I put this in the App Delegate of an actual iOS app:
I saw the two
println
messages, proving that there was no retain cycle. If I deleted theunowned
keyword and ran again, I didn't see the twoprintln
messages, proving that there was a retain cycle. Thus we know thatunowned
does what it is advertised to do. That's all we need to know. What works or doesn't work in a playground isn't terribly important.You can also create a console app (New Project > macOS > Command Line Tool) This way, you don't need to have simulator running when you run your code.