Method overloading in Objective-C?

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:").


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));

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