How can I distinguish one method name from two protocols in a class implementation?

I have two protocols

@protocol P1 -(void) printP1; -(void) printCommon; @end @protocol P2 -(void) printP2; -(void) printCommon; @end 

Now I implement these two protocols in one class

 @interface TestProtocolImplementation : NSObject <P1,P2> { } @end 

How can I write a method implementation for "printCommon". When I try to execute the implementation, I have a compile time error.

Is it possible to write a method implementation for "printCommon".

+6
source share
2 answers

A general solution is to separate a common protocol and force derived protocols to implement a common protocol, for example:

 @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 the types that accept P1 and P2 must also accept PrintCommon methods to execute the accept, and you can safely pass the parameters NSObject<P1>* through NSObject<PrintCommon>* .

+14
source

the following code worked for me:

 @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 are developing such a solution, try to avoid such situations.

+2
source

Source: https://habr.com/ru/post/900679/


All Articles