Do I need to check if weak self is nil in blocks?
I create weakSelf pointer like:
__weak typeof(self) weakSelf = self;
and in the b
Weak references do not retain the referred object. If none else is retaining it, the object is released and the weak references refers to nil
.
Therefore it is possible that your code is executed with a weakSelf
that refers nil
. But this is no reason to check for it at all. Especially in Objective-C you use a defined behavior, if you send a message nil
. I. e. it is perfect code, if you set a property using a nil
reference. It simply goes to nowhere.
Of course sometime you do not want to interact with nil
. In such a case you have to check for it.
BTW: You need weakSelf
only in some, rarely circumstances. It is an urban legend that in closures in general references to self
has to be weak to prevent retain cycles. It has not been true, it is not true and it will never be true.