I always thought that one cannot declare an object property in a category. Until my partner did it in our app's code, and it seemed to work.
I went on a SO and Google binge to try to explain to him that no, Objective-C categories can only be used to add methods, not properties. I found questions such as:
- Setting New Property In Category Interface Implementation (look at the accepted answer)
- Can I add a property for a method not in my category?
But then I found this link on Apple's site that contains the following about the @property declaration:
A property declaration begins with the keyword @property. @property can appear anywhere in the method declaration list found in the @interface of a class. @property can also appear in the declaration of a protocol or category. (emphasis added)
I know that this doesn't work:
@interface MyClass ()
NSInteger foobar;
- (void) someCategorizedMethod;
@end
But this compiles:
@interface MyClass ()
@property NSInteger foobar;
- (void) someCategorizedMethod;
@end
My question is (a) what's the best practice here? and (b) is this something that is new to Objective-C 2.0, and instead of using a "real" iVar, it simply uses associative storage behind the scenes to make this work?