__strong keyword, when to use, when not to use?

Discussion in 'Mac Programming' started by BadWolf13, Apr 15, 2014.

  1. BadWolf13 macrumors 6502

    Joined:
    Dec 17, 2009
    #1
    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?
     
  2. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #2
    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.
     
  3. BadWolf13 thread starter macrumors 6502

    Joined:
    Dec 17, 2009
    #3
    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?
     
  4. Madd the Sane macrumors 6502a

    Madd the Sane

    Joined:
    Nov 8, 2010
    Location:
    Utah
    #4
    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.
     
  5. Menge macrumors 6502a

    Menge

    Joined:
    Dec 22, 2008
    Location:
    Amsterdam
    #5
    __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.
     
  6. Madd the Sane macrumors 6502a

    Madd the Sane

    Joined:
    Nov 8, 2010
    Location:
    Utah
    #6
    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).
     

Share This Page