Reputation: 135
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
Reputation: 126
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
Reputation: 4257
Just rename method in first protocol to testingMethod1 and method in second protocol to testingMethod2
Upvotes: 1
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