Reputation: 12852
How does the Objective-C runtime treat a property that is defined as weak
in a class, but a private category on the class defines a property of the same name and type as strong
?
Will code running in the context of the Category use the original (weak) modifier, or will it use the category defined modifier (strong)?
For example:
Name.m
@property (weak, nonatomic) NSString *name;
NameTests.m
@interface Name (Test)
@property (strong, nonatomic) NSString *name;
@end
Upvotes: 10
Views: 758
Reputation: 34263
When adding properties with ambiguous names at runtime (which happens when you extend a class with a category), the behavior is undefined as to which method implementation is used.
This is simply a naming clash that can be avoided by prefixing your category method/property. There are some details about prefixing in the "Avoid Category Method Name Clashes" section of Apple's "Programming With Objective-C" Guidelines.
When you build a simple test project you could probably observe, that the runtime randomly uses one of the two implementations. (When testing this, avoid constant strings for your name property, because they won't expose the behaviour)
By using a class extension instead of a named category, the compiler will generate an "Illegal redeclaration of property" error.
Upvotes: 0
Reputation: 25619
In a property declaration, weak
only applies to the synthesized setter method, if any, and synthesized instance variable, if any. If neither of those are synthesized then weak
has no effect.
If the setter and instance variable are synthesized, the question then is: which property declaration is the compiler using to synthesize the setter and instance variable?
The compiler will never synthesize a property declared in a named category. So in your example, name
is a weak property.
Upvotes: 2