Reference properteries declared in a protocol and implemented in the anonymous category?
- by Heath Borders
I have the following protocol:
@protocol MyProtocol
@property (nonatomic, retain) NSObject *myProtocolProperty;
-(void) myProtocolMethod;
@end
and I have the following class:
@interface MyClass : NSObject {
}
@end
I have a class extension declared, I have to redeclare my protocol properties here or else I can't implement them with the rest of my class.
@interface()<MyProtocol>
@property (nonatomic, retain) NSObject *myExtensionProperty;
/*
* This redeclaration is required or my @synthesize myProtocolProperty fails
*/
@property (nonatomic, retain) NSObject *myProtocolProperty;
- (void) myExtensionMethod;
@end
@implementation MyClass
@synthesize myProtocolProperty = _myProtocolProperty;
@synthesize myExtensionProperty = _myExtensionProperty;
- (void) myProtocolMethod {
}
- (void) myExtensionMethod {
}
@end
In a consumer method, I can call my protocol methods and properties just fine. Calling my extension methods and properties produces a warning and an error respectively.
- (void) consumeMyClassWithMyProtocol: (MyClass<MyProtocol> *) myClassWithMyProtocol {
myClassWithMyProtocol.myProtocolProperty; // works, yay!
[myClassWithMyProtocol myProtocolMethod]; // works, yay!
myClassWithMyProtocol.myExtensionProperty; // compiler error, yay!
[myClassWithMyProtocol myExtensionMethod]; // compiler warning, yay!
}
Is there any way I can avoid redeclaring the properties in MyProtocol within my class extension in order to implement MyProtocol privately?