Reputation: 77596
While creating properties, is it correct to replace all retains
with strong
, and all assigns
with weak
?
I'm switching to ARC any helpful tips?
Upvotes: 2
Views: 481
Reputation: 8512
As far as I know, strong
and retain
are synonyms, so they do exactly the same.
Edit: Also unsafe_unretained
is synonym for assign
, as nielsbot pointed out.
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.
Upvotes: 2
Reputation: 3184
Read Transitioning to ARC Release Notes
Use Xcode's guide: Edit > Refactor > Convert to Objective-C ARC.
At first, it may report various issues( in the precheck building phase), just try fixing all issues, try again and (build and fail )again, and it would be done mostly smoothly in the end when all issues are fixed, leaving your code with ARC.
Note that the pre-checking rules are more tough than usual build settings.
Upvotes: 4
Reputation: 943
Short answer is yes. strong
is the ARC equivalent of retain
, and weak
is the equivalent of assign
, only it is also zeroing (sets the pointer to nil
if the object is deallocated, preventing potential EXC_BAD_ACCESS
crashes), so it is even better than assign
. The Transitioning to ARC Release Notes page, as already mentioned, provides more details if you're interested.
Upvotes: 1