Search code examples
iphoneobjective-cxcodeautomatic-ref-countingprivate-members

Are private variables in Objective-C strong?


So searching around Stack Overflow this seems to be how to make private variables in Objective-C:

@interface ClassName()
{
@private
    NSArray* private;
}
@property (strong, nonatomic) NSArray* public;
@end

Now this is where I get confused. The property is declared as (strong, nonatomic), but the private variable has nothing of the sort. So how does arc know if it's strong or not?


Solution

  • In the case of a property, the ownership of the associated instance variable is implied by the ownership of the property:

    See http://clang.llvm.org/docs/AutomaticReferenceCounting.html:

    If a property is synthesized, then the associated instance variable is the instance variable which is named, possibly implicitly, by the @synthesize declaration. If the associated instance variable already exists, then its ownership qualification must equal the ownership of the property; otherwise, the instance variable is created with that ownership qualification.

    Generally, Objective-C objects are by default strong:

    If an object is declared with retainable object owner type, but without an explicit ownership qualifier, its type is implicitly adjusted to have __strong qualification.

    Note that since the LLVM 4.0 compiler (Xcode 4.4), the @synthesize statement and the associated instance variable is created automatically, so you need only to declare the property.