I have a category on a class I made, and that category conforms to a protocol, which requires a property in its implementation. However, because I'm in a category, I cannot synthesize the property in the implementation of the category. Because of this, I'm stumped on how to implement the setter method (when I keep the protocol's property readonly it works fine, since all I need is an accessor method).
This is my protocol:
@protocol SomeProtocol <NSObject>
@property (nonatomic) BOOL didDisplayRecommendation;
@end
I know if I do this I'll get an infinite loop:
- (void)setDidDisplayRecommendation:(BOOL)didDisplayRecommendation
{
self.didDisplayRecommendation = didDisplayRecommendation;
}
But when I try this I get a compiler error:
- (void)setDidDisplayRecommendation:(BOOL)didDisplayRecommendation
{
_didDisplayRecommendation = didDisplayRecommendation;
}
Note that didDisplayRecommendation is the property in the protocol. What's the best way of getting around this? Thanks in advance!
You are not allowed to add instance variables to a class through categories, see https://stackoverflow.com/a/13000930/171933
Since you need a variable to hold the value of didDisplayRecommendation
, you are out of luck of doing this with a category (unless you want to get dirty https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html#//apple_ref/doc/uid/TP40001418-CH3g-SW5).
I'd recommend re-thinking your architecture to see if you really need to use categories. Subclasses or even Mixins might be the better way to go.