softempire
softempire

Reputation: 135

what should do for same method in multiple protocols? (Objective C)

Say I have protocols ProA and ProB. They both have a method -(void) testingMethod;

And I have another class ClassAB which implementes both of these protocols. What should I do for the testingMethod?

I know we can implement only one testingMethod. But what should I do if I want to have two testingMethods in class ClassAB, for ProA and Prob respectively?

Upvotes: 4

Views: 2018

Answers (3)

There's also the case where you're simply using two libraries that define protocols with methods that have the same name, as a coincidence, so you can't modify them.
In that case you would need to create two classes, one implementing each protocol, let's say ClassA for ProA and ClassB for ProB, and then pass your instance of ClassAB to each of them.
When ClassA receives a call from testingMethod you make it call ClassAB's method, for example classAB testingMethodA.

If it's not clear tell me and I write the implement.

Upvotes: 3

rakeshNS
rakeshNS

Reputation: 4257

Just rename method in first protocol to testingMethod1 and method in second protocol to testingMethod2

Upvotes: 1

Ankit Srivastava
Ankit Srivastava

Reputation: 12405

you should separate the common protocol method in a different protocol and create two new protocols implementing the earlier protocol.. see it is very well explained here.. https://stackoverflow.com/a/7992461/919545

Upvotes: 2

Related Questions