There are two new memory management attributes for properties introduced by ARC, strong
and weak
.
Apart from copy
, which is ob
As far as I know, strong
and retain
are synonyms, so they do exactly the same.
Then the weak
is almost like assign
, but automatically set to nil after the object, it is pointing to, is deallocated.
That means, you can simply replace them.
However, there is one special case I've encountered, where I had to use assign
, rather than weak
. Let's say we have two properties delegateAssign
and delegateWeak
. In both is stored our delegate, that is owning us by having the only strong reference. The delegate is deallocating, so our -dealloc
method is called too.
// Our delegate is deallocating and there is no other strong ref.
- (void)dealloc {
[delegateWeak doSomething];
[delegateAssign doSomething];
}
The delegate is already in deallocation process, but still not fully deallocated. The problem is that weak
references to him are already nullified! Property delegateWeak
contains nil, but delegateAssign
contains valid object (with all properties already released and nullified, but still valid).
// Our delegate is deallocating and there is no other strong ref.
- (void)dealloc {
[delegateWeak doSomething]; // Does nothing, already nil.
[delegateAssign doSomething]; // Successful call.
}
It is quite special case, but it reveal us how those weak
variables work and when they are nullified.