How can I differentiate the same method name of two protocols in a class implementation?

后端 未结 2 1222
栀梦
栀梦 2021-02-09 05:42

I have two protocols

@protocol P1

-(void) printP1;

-(void) printCommon;

@end


@protocol P2

-(void) printP2;

-(void) printCommon;
@end

Now

2条回答
  •  长情又很酷
    2021-02-09 06:23

    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* through NSObject* parameters.

提交回复
热议问题