0

I have an iOS app that uses SDK "Foo" and SDK "Bar". SDK "Foo" allows me to set a delegate that responces to

-(void)request:(Foo *)request didFailWithError:(NSError *)error

and "Bar" allows the same:

-(void)request:(Bar *)request didFailWithError:(NSError *)error

I have a class that wants to be a delegate for both SDKs:

@interface MyApp : NSObject<FooDelegate, BarDelegate>

@interface MyApp

-(void)request:(Foo *)request didFailWithError:(NSError *)error
{
    NSLog(@"Foo failed");
}
-(void)request:(Bar *)request didFailWithError:(NSError *)error
{
    NSLog(@"Bar failed");
}

and compiler blames me for "Duplicate declaration of method 'request:didFailWithError:'"

How do I fix it?

Nick
  • 3,205
  • 9
  • 57
  • 108

3 Answers3

5

You have to declare the method once and determine the class of the delegated object:

@implementation MyApp

- (void)request:(id)request didFailWithError:(NSError *)error
{
    if ([request isKindOfClass:[Foo class])
    {
        Foo *fooRequest = (Foo *)request;
        NSLog(@"Foo failed");
    }
    else if ([request isKindOfClass:[Bar class]])
    {
        Bar *barRequest = (Bar *)request;
        NSLog(@"Bar failed");
    }
}

@end

The reason your code didn't work is that Objective C does not support true method overloading, since it is dynamic. Even though the first parameter is typed Foo, technically, an object of any class could be passed in. It's also worth mentioning that ObjC does method lookup at runtime essentially using strings - the selector for both of these methods is just "request:didFailWithError:".

Austin
  • 5,625
  • 1
  • 29
  • 43
  • Whilst this is an alternative I can't bring myself to give a +1 without an explanation of why you would have to do it this way, such as because objective-c doesn't support overloading. Just asking for a brief explanation nothing big – Popeye May 21 '14 at 13:42
  • @Austin: what exactly does it resolve? I guess the OP is facing a linker error. – egghese May 21 '14 at 13:43
  • 2
    @JeslyVarghese OP is trying to delegate for two objects whose delegate protocol declare methods with identical signatures. As far as the compiler is concerned, these are the _same method_. – Austin May 21 '14 at 13:45
  • The issue of who delegated the task arise at run-time I believe. Since overloading is not supported by Objective-C, this issue never arises is my guess. Then the solution of differentiating between objects which delegated will be irrelevant. The o/p seeks on resolving a linker error, but he is quite unclear about it. So, I don't find the answer quite in context. Nothing big, just asking to know if am missing out anything. – egghese May 21 '14 at 13:50
2

The reason behind this is Objective does not support method overloading :) due to its dynamic nature.

Gajendra Rawat
  • 3,673
  • 2
  • 19
  • 36
0

Each and every member functions are by default virtual in nature in Objective-C. So, there is no method overloading is possible. As far as arguments are same, they are identical functions for compiler. Either you can change the name of the argument or you need to identify object type in side the method as suggested by Austin.

Apurv
  • 17,116
  • 8
  • 51
  • 67