Reference properteries declared in a protocol and implemented in the anonymous category?

Posted by Heath Borders on Stack Overflow See other posts from Stack Overflow or by Heath Borders
Published on 2011-01-31T04:08:14Z Indexed on 2011/01/31 7:25 UTC
Read the original article Hit count: 137

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about cocoa