Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

BadWolf13

macrumors 6502
Original poster
Dec 17, 2009
271
0
I know this is probably something most people did months ago, but I've started converting some of my older apps to ARC. One thing I noticed, is that sometimes, the converter adds that __strong qualifier to my variable declarations, and sometimes it doesn't. Anyone got any idea why some variables need this and others don't?
 
Do you have an Apple Developer account? There are some nice presentations from the Apple WWDC that talk about "strong" vs. "weak" and how it applies to reference counting.

The short (and slightly misleading) answer is that "strong" pointer means your code wants to count as a reference against that object where "weak" means it doesn't. But not only that, if a pointer is declared "weak", it's possible the run-time will clear that pointer if the object it was pointing to was deallocated.

The classic "weak" pointer is pointer to a delegate object. Your class doesn't "care" if there's a delegate or not. If the delegate object should be deallocated for some reason, your object shouldn't keep it around. I think Outlets are typically marked as "weak" as well. The other reason to use "weak" is to prevent reference loops between objects if two (or more) objects point to each other. Two objects that point to each other with strong references could never be deallocated which may not be what you want.

If you were using "retain" properties before, they should be "strong" in ARC. "assign" properties may be strong in some cases but could be "weak" in others. There's not a straight-forward mapping. There's probably a released Apple doc about this somewhere.
 
Maybe I didn't explain myself properly. I read up on the documentation, and I know the difference between "strong" and "weak."

What I don't understand is, why did the automatic conversion process add __strong to some declarations, but other objects didn't get any qualifier in terms of strong and weak.

Also, if an object is declared as neither __strong, nor __weak, what is it?
 
Weak is not synonymous with assign: unsafe_unretained is synonymous with assign. Weak does a bit more than assign: it blanks the pointer that was declared weak when no other strong references (when the object is dealloced) are present.

There is another ARC declaration that can be used: autoreasing. This is not used for declaring variables, but passing variables. By default, objects passed by referenced are declared thus.

As for when an object is declared without one of the above qualifiers, it will nine times out of ten be strong. The exceptions are when returning from a function or call that isn't an initializer (doesn't begin with copy… or init…), when the object is passed by value (most likely an NSError object, like so:
Code:
…error:(NSError**)outError
), when an object variable is declared as an IBOutlet. Note that this list is not exhaustive.
 
Maybe I didn't explain myself properly. I read up on the documentation, and I know the difference between "strong" and "weak."

What I don't understand is, why did the automatic conversion process add __strong to some declarations, but other objects didn't get any qualifier in terms of strong and weak.

Also, if an object is declared as neither __strong, nor __weak, what is it?
__strong has the same semantics as strong. Just for iVars or vars in the functions.
__weak has the same semantics as weak. Like Madd the Sane said, weak is kind of like assign but with auto-nilling pointers.
 
Also, some classes cannot be weak-referenced. NSTextView(OS X) is one of them (probably why, when you drag a new label to an NSView, Xcode uses NSTextField instead).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.