Overriding in Obj-C and Swift works the same way as in Java (in Swift even denoted by override
keyword).
Obviously there is a lot of confusion regarding overloading. Let's start with Swift. In Swift, in general it works in the same way as in Java:
func myFunction(a: Int)
func myFunction(a: String)
The same method name, accepting different parameters. The compiler will decide on which method to call depending on the type of the parameter. However, when adding additional parameters:
func myFunction(a: Int, b: Int)
the method name actually changes from myFunction(_:)
to myFunction(_:b:)
so in the traditional sense, this shouldn't be called overloading but I belive Swift is using only the first part of the name (myFunction
) in some cases to differentiate between methods so this actually could be overloading, too. I am a bit unsure about this case.
In Obj-C, there is no overloading.
We cannot declare the following in Obj-C:
@interface MyClass : NSObject
- (void)myMethod:(id)parameterA;
- (void)myMethod:(NSInteger)parameterA;
@end
and when changing the number of parameters, we also have to change the method name
@interface MyClass : NSObject
- (void)myMethod:(id)parameterA;
- (void)myMethod:(NSInteger)parameterA withAnotherParam:(id)parameterB;
@end
Note the method names are -myMethod:
and -myMethod:withAnotherParam:
respectively. When the method name is different, it's not overloading.
However, even in Obj-C we can use the plain old C functions and they can be overloaded:
void functionA(int a) {
...
}
void functionA(int a, int b) {
...
}