Search code examples
objective-cmemory-managementdeclared-property

Can the memory management of a property change if it's redefined in a class extension?


If I have a property like this:

//test.h

@interface test
@property (nonatomic, readonly, weak) NSObject x;
@end

redefined in the implementation file to be read/write:

// test.m
@interface test ()
@property (nonatomic, readwrite) NSObject x;
@end

I used weak in .h, but I said nothing in the extension, will the property keep the 'weak' specifier, or will it change to 'strong'?

Will the keywords strong/assign/weak be overwritten when the property is redefined?


Solution

  • A simple test with Xcode 5.1.1 shows the weak attribute is kept. The same is true for the assign and strong attributes - you can specify them in the .h and omit them in the .m, if you do include them in the .m the two must match.

    Having said that, I do not know if this is documented anywhere. But then the semantics of Objective-C are not formally defined anywhere either. So use at your own risk.

    Recommendation: just repeat it.