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:").
Is function overloading possible in Objective C?
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.
Function Overloading in Objective-C?
Is the method with two params in the second case changing the Method Name ?
Yes. A method name is the compound of all its parameter prefixes including the colons. So your two methods are updateValue:
and updateValue::
.
HTH
Emulating method overloading in Objective-C
Here is the final design I used, resolving the types using reflection. I am still using classes as containers of overloaded functions, essentially using those container types as vtables:
#define CMOverloadingMethod(_return, _left, _right) \
+ (_return *)_left:(_left *)left _right:(_right *)right
#define CMOverloadedMethod(_type, _object) \
return [self callOverloadedMethod:[_type class] withObject:(_object)]
- (id)callOverloadedMethod:(Class)method withObject:(id)other
{
Class thisClass = [self class];
Class nextClass = [other class];
Class methodClass = object_getClass(method);
// Find the appropriate overload:
Class thisClass2 = Nil;
do
{
thisClass2 = thisClass;
Class otherClass = nextClass;
Class otherClass2 = Nil;
do
{
otherClass2 = otherClass;
SEL selector = NSSelectorFromString(MSSTR(@"%@:%@:", NSStringFromClass(thisClass), NSStringFromClass(otherClass)));
if (class_respondsToSelector(methodClass, selector))
return objc_msgSend(method, selector, self, other);
else
otherClass = class_getSuperclass(otherClass);
} while (otherClass != otherClass2 && otherClass && otherClass2);
thisClass = class_getSuperclass(thisClass);
} while (thisClass != thisClass2 && thisClass && thisClass2);
[NSException raise:NSInvalidArgumentException format:@"Cannot resolve overloaded method in class %@ for objects typed %@ and %@.", NSStringFromClass(method), NSStringFromClass([self class]), NSStringFromClass([other class])];
return nil;
}
Method Overloading is possible in objective c, am i right?
In Objective C, the signature of a method includes the parameters.
Your two methods:
- (void)methodoverloading
and
- (void)methodoverloading:(int)
have different signatures (methodoverloading
vs. methodoverloading:
), and thus are different methods.
What people mean when they say that Objective C doesn't support overloading is that you can't define:
- (void)method:(int)arg
and
- (void)method:(NSString *)arg
and have the compiler choose between them based on the type you provide.
Using Swift clases with overloaded methods in Objective C
As of swift2.0 you can differentiate if you're planning on using both in Objective C:
@objc(randomLoInt:HiInt:)
func random(#lower: Int , upper: Int) -> Int {
return lower + Int(arc4random_uniform(UInt32(upper - lower + 1)))
}
@objc(randomLoFlt:HiFlt:)
func random(#lower: CGFloat , upper: CGFloat) -> CGFloat {
return CGFloat(arc4random() % 1) / 0
}
or if you're not going to use this in your ObjC side of the project at all, just use:
@nonobjc
func random(#lower: Int , upper: Int) -> Int {
return lower + Int(arc4random_uniform(UInt32(upper - lower + 1)))
}
@nonobjc
func random(#lower: CGFloat , upper: CGFloat) -> CGFloat {
return CGFloat(arc4random() % 1) / 0
}
Objective-C resolve functions overloading
You have to declare the method once and determine the class of the delegated object:
@implementation MyApp
- (void)request:(id)request didFailWithError:(NSError *)error
{
if ([request isKindOfClass:[Foo class])
{
Foo *fooRequest = (Foo *)request;
NSLog(@"Foo failed");
}
else if ([request isKindOfClass:[Bar class]])
{
Bar *barRequest = (Bar *)request;
NSLog(@"Bar failed");
}
}
@end
The reason your code didn't work is that Objective C does not support true method overloading, since it is dynamic. Even though the first parameter is typed Foo
, technically, an object of any class could be passed in. It's also worth mentioning that ObjC does method lookup at runtime essentially using strings - the selector for both of these methods is just "request:didFailWithError:"
.
Why Objective-C doesn't support method overloading?
The distinction that's relevant here is not between compiled and interpreted languages, but between statically typed (Java, C#) and dynamically typed (Ruby, Python, Objective-C). In a dynamically typed language, type information is very often not known until runtime. At runtime, all objects are statically typed as id
in Objective-C.
Additionally, a core idea in dynamically typed OO languages is that you should not care what type an object is as long as it responds to the messages you want to send. So overloading based on type would fly right in the face of that.
Related Topics
How to Change Color of Single Word in Uitextview and Uitextfield
iOS - Calling App Delegate Method from Viewcontroller
Programmatically Navigate to Another View Controller/Scene
How to Implement the Hashable Protocol in Swift for an Int Array (A Custom String Struct)
Google Sign-In Crashes on iOS 9 Attempting to Call Canopenurl
Force Landscape Orientation in Uiimagepickercontroller
Objective-C Find Caller of Method
Uiscrollview: Paging Horizontally, Scrolling Vertically
Delete All Keys from a Nsuserdefaults Dictionary iOS
Detecting Collisions in Sprite Kit
Add Bottom Border Line to UI Textfield View in Swiftui/Swift/Objective-C/Xamarin
How to Set Up Push Notifications in Swift
Duplicate Symbol Error in Objective-C Build
Allow Only Alphanumeric Characters for a Uitextfield
How to Integrate Facebook Without Redirect Safari Browser in iOS App with Latest Fbsdk