7

Is function overloading possible in Objective C ?
Well,Most of the programmers says no,
But it looks like possible,
for example:

-(int)AddMethod:(int)X :(int)Y
{
    return X + Y;
}
-(int)AddMethod:(int)X
{
    return X;
}

to call 1st one write [self AddMethod :3];
to call last one write [self AddMethod: 3 :4];

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
Matrix
  • 7,477
  • 14
  • 66
  • 97
  • i have read many times programmers saying that function overloading is not possible in objective C. – Matrix Sep 08 '10 at 09:08
  • 6
    This is not overloading, your methods have different selectors (=names). One is `AddMethod:` and the other is `AddMethod::` – Sven Sep 08 '10 at 09:34

7 Answers7

24

Method overloading is not possible in Objective-C. However, your example actually will work because you have created two different methods with different selectors: -AddMethod:: and AddMethod:. There is a colon for each interleaved parameter. It's normal to put some text in also e.g. -addMethodX:Y: but you don't have to.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • 2
    You don't *have* to label the second parameter, but really… you have to. There's no reason to make your code intentionally obtuse. – kubi Sep 08 '10 at 10:34
  • 3
    @kubi: Completely agree. It's the same kind of "don't have to" as in "you don't have to use letters in C variable names, you can just use strings of underscores". – JeremyP Sep 08 '10 at 13:45
  • im confused... i thought that counted as method overloading (either different number of arguments or types) EDIT: ok, it looks like only types counts as overloading for you guys... In so many examples online, I see that even different signature counts as "overloading." Sigh... – dtc Jul 21 '13 at 00:37
4

No, it is not, mostly because Objective-C doesn't use functions, it uses methods.

Method overloading, on the other hand, is possible. Sort of.

Consider, if you will, a class with a method take an argument on the form of either an NSString * or a const char *:

@interface SomeClass : NSObject {

}

- (void)doThingWithString:(NSString *)string;
- (void)doThingWithBytes:(const char *)bytes;

@end

While the method itself won't go around choosing the proper method with a given input; one could still say that doThing: was overloaded, at least in the sense that the two methods taking a different parameter to achieve the same functionality.

Williham Totland
  • 28,471
  • 6
  • 52
  • 68
  • Answers must be at least 15 characters long; so I fleshed mine out a bit. ;) – Williham Totland Sep 08 '10 at 09:15
  • 5
    The example is not even *sort of* overloading, those are simply two different methods for which the naming implies some semantic relation. Its just the commonly used alternative in the absence of overloading. – Georg Fritzsche Sep 08 '10 at 09:17
  • 2
    @Georg: Which, in essence, is how overloading is implemented in, say, C++: Overloaded methods have the same name in code, but their actual, mangled names are different, and the appropriate one is chosen at compile time. The same principle is at work here, the only difference being that the programmer chooses the correct "mangled" name, not the compiler. – Williham Totland Sep 08 '10 at 09:20
  • 1
    @Williham Totland: Name mangling is just an implementation detail. There's nothing to say that a C++ compiler/linker has to use name mangling. – JeremyP Sep 08 '10 at 09:50
  • Besides name mangling being an implementation detail, overloading is a language feature with all the bundled benefits (e.g. in C++ usage of only one function name in a generic context). – Georg Fritzsche Sep 08 '10 at 10:01
  • @Georg Fritzsche: some people would argue that's not a benefit :) – JeremyP Sep 08 '10 at 13:50
2

Technically, method overloading is not possible in Objective-C. In practice you can usually achieve the same results, also in cases where you couldn't in C++. In Objective-C, method names include a colon in front of each argument, and the colons are PART OF THE METHOD NAME, which means your example uses different method names. In practice this becomes a sort of pseudo-named-parameters functionality, and you can get a pseudo method overloading by argument FUNCTION rather than argument TYPE. In most cases this will actually be more useful, but it is not method overloading in the strict sense, because the method names are different.

Example:

-(void)makeSquareWithX1:(float)x1 Y1:(float)y1 X2:(float)x2 Y2:(float)y2;  
-(void)makeSquareWithX1:(float)x1 Y1:(float)y1 width:(float)width height:(float)height;  

This would work in Objective-C, but you couldn't get similar functionality in C++, because the argument number and types are the same, only argument functions are different. In some few cases the C++ model can achieve more useful functionality. This is demonstrated by the NSKeyedArchiver class:

-(void)encodeFloat:(float)realv forKey:(NSString *)key  
-(void)encodeInt32:(int32_t)intv forKey:(NSString *)key  

Here they had to make argument types part of the moethod name, which is ugly. If I could choose between C++ overloading and Objective-C "overloading", I would still choose the latter.

Balthazar
  • 21
  • 2
2

You could start with a generic method which routes based on the type of the object you pass in.

- (void)doSomething:(id)obj;

Then you can check the type to see if it's NSData or UIImage and route it internally to the appropriate methods.

- (void)doSomethingWithData:(NSData *)data;
- (void)doSomethingWithImage:(UIImage *)image;

You can also choose to only support expected types and gracefully decline to process unsupported types or fail explicitly.

Look up NSAssert1, NSAssert2, etc for another approach.

Brennan
  • 11,546
  • 16
  • 64
  • 86
0

Note that if you need / want to overload functions when combining Objective-C with C++ it is possible. I only mention this because in XCode you need to change your .m file to a .mm file for it to treat it this way, which tripped me up for a few minutes.

Example header:

void addMethod(NSInteger a);
void addMethod(NSInteger a, NSInteger b);

If you are including this in a .m file you'll get a warning:

Conflicting types for 'addMethod'

Changing your .m file into a .mm clears the issue.

Ben Flynn
  • 18,524
  • 20
  • 97
  • 142
0

You can overload C functions with newer compilers - Syntax is a little clunky though (nothing a quick #define can't fix), but it works well.

function overloading in C

Community
  • 1
  • 1
Steazy
  • 383
  • 3
  • 12
-1

Categories provide another way to emulate c-style method overloading in Objective C. As an example, consider the following interface declarations:

@interface MyClass
{
}
@end

@interface MyClass ( MethodVersion1 )
- (void) doSomething : (int) val;
@end

@interface MyClass ( MethodVersion2 )
- (void) doSomething : (double) val;
@end

The different parameter types are resolved by the Categories.

trotsky
  • 1
  • 1