I am curious if the next code will lead to the strong reference cycle?
enum Type {
case some(obj:Any)
}
class Entity {
var type:Type
init() {
type = Type.some(obj:self)
}
}
Yes. Any
is implicitly strong. If you pass a reference type, it will be a strong reference. It's not quite a "cycle" since nothing "retains" an enum, but as long as the value exists (or any copy of the value), it will hold onto Entity
and prevent it from being deallocated.
Imagine if it were not true. What would .some(obj: NSObject())
contain? If Type.some
did not increase the retain count, the NSObject
would vanish. (Since this is very similar to an Optional
, that would be very surprising, since many T?
would immediately become nil
.)
BTW, this is easily and usefully explored by creating a deinit
method on Entity
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With