Method overloading in Objective-C?

IosObjective COverloading

Ios Problem Overview


As far as my knowledge, Objective-C does not support method overloading. What can be the alternative for this in Objective-C? Or should I always use different method name?

Ios Solutions


Solution 1 - Ios

Correct, objective-C does not support method overloading, so you have to use different method names.

Note, though, that the "method name" includes the method signature keywords (the parameter names that come before the ":"s), so the following are two different methods, even though they both begin "writeToFile":

-(void) writeToFile:(NSString *)path fromInt:(int)anInt;
-(void) writeToFile:(NSString *)path fromString:(NSString *)aString;

(the names of the two methods are "writeToFile:fromInt:" and "writeToFile:fromString:").

Solution 2 - Ios

It may be worth mentioning that even if Objective-C doesn't support method overloading, Clang + LLVM does support function overloading for C. Although not quite what you're looking for, it could prove useful in some situations (for example, when implementing a slightly hacked (goes against encapsulation) version of the visitor design pattern)

Here's a simple example on how function overloading works:

__attribute__((overloadable)) float area(Circle * this)
{
    return M_PI*this.radius*this.radius;
}

__attribute__((overloadable)) float area(Rectangle * this)
{
    return this.w*this.h;
}

//...
//In your Obj-C methods you can call:
NSLog(@"%f %f", area(rect), area(circle));

Solution 3 - Ios

David is correct in that method overloading is not supported in Objective-C. It is similar to PHP in that sense. As he also points out, it is common practice to define two or more methods with different signatures in the manner he examples. However, it is also possible to create one method using the "id" type. Via the "id" type, you can send any object (and any primitives using the NSNumber class) to the method and then from within the method itself you can test its type and throw the appropriate exception if necessary. Although this does have a minor performance hit, it will most likely be nominal or insignificant unless you are processing large quantities of data.

- (void) writeToFile: (NSString *)path fromObject: (id)object {
    if (!([object isKindOfClass: [NSNumber class]] || [object isKindOfClass: [NSString class]])) {
         @throw [NSException exceptionWithName: @"InvalidArgumentException" reason: @"Unrecognized parameter type." userInfo: nil];
    }
}

This is also a beautiful place to implement a protocol to enforce the object type, which can be done like so:

(id<MyProtocol>)object

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsuseView Question on Stackoverflow
Solution 1 - IosDavid GelharView Answer on Stackoverflow
Solution 2 - IosRad'ValView Answer on Stackoverflow
Solution 3 - IosZiminjiView Answer on Stackoverflow