I have two protocols
@protocol P1
-(void) printP1;
-(void) printCommon;
@end
@protocol P2
-(void) printP2;
-(void) printCommon;
@end
Now, I am implementing these two protocols in one class
@interface TestProtocolImplementation : NSObject <P1,P2>
{
}
@end
How can i write method implementation for "printCommon". When i try to do implementation i have compile time error.
Is there any possibility to write method implementation for "printCommon".
The common solution is to separate the common protocol and make the derived protocols implement the common protocol, like so:
@protocol PrintCommon
-(void) printCommon;
@end
@protocol P1 < PrintCommon > // << a protocol which declares adoption to a protocol
-(void) printP1;
// -(void) printCommon; << available via PrintCommon
@end
@protocol P2 < PrintCommon >
-(void) printP2;
@end
Now types which adopt P1
and P2
must also adopt PrintCommon
's methods in order to fulfill adoption, and you may safely pass an NSObject<P1>*
through NSObject<PrintCommon>*
parameters.
for me the following code did worked:
@protocol P1
- (void) method1;
@end
@protocol P2
- (void) method1;
- (void) method2;
@end
@interface C1 : NSObject<P1, P2>
@end
@implementation C1
- (void) method1
{
NSLog(@"method1");
}
- (void) method2
{
NSLog(@"method2");
}
@end
Compiler user: Apple LLVM 3.0
But if you're designing a solution like this, try to avoid such situations.