Currently, the compiler allows marking as weak
the computed properties, but since they don't store values, the weak
keyword makes no sense.
class Human {
var _friend: Human?
weak var friend: Human? {
set {
self._friend = newValue
}
get {
return self._friend
}
}
deinit {
print("deinit")
}
}
var human: Human? = Human()
var friend: Human? = Human()
human?.friend = friend
friend?.friend = human
human?.friend
human = nil
friend = nil
In the above example, as you can see, the weak have no effect, as result, we have a memory leak. If the compiler disallows the weak
keyword, we can notice that the weak
mark should be on another property.