Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C properties - strong and weak declarations in categories

Tags:

objective-c

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
like image 346
Andy Obusek Avatar asked Jul 10 '14 16:07

Andy Obusek


1 Answers

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.

like image 143
Darren Avatar answered Dec 07 '22 22:12

Darren