Is function overloading possible in Objective C?
Solution 1
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.
Solution 2
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.
Solution 3
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.
Solution 4
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.
Related videos on Youtube
Comments
-
Matrix over 1 year
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];
-
Sven about 13 yearsThis is not overloading, your methods have different selectors (=names). One is
AddMethod:
and the other isAddMethod::
-
-
Williham Totland about 13 yearsAnswers must be at least 15 characters long; so I fleshed mine out a bit. ;)
-
Georg Fritzsche about 13 yearsThe 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.
-
Williham Totland about 13 years@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.
-
JeremyP about 13 years@Williham Totland: Name mangling is just an implementation detail. There's nothing to say that a C++ compiler/linker has to use name mangling.
-
Georg Fritzsche about 13 yearsBesides 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).
-
Ajay Choudhary about 13 yearsYou don't have to label the second parameter, but really… you have to. There's no reason to make your code intentionally obtuse.
-
JeremyP about 13 years@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 about 13 years@Georg Fritzsche: some people would argue that's not a benefit :)
-
dtc over 10 yearsim 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...