可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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];
回答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.
回答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.
回答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.
回答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.
回答5:
Note that if you need / want to overload functions when combining Objective-C with C++ it is possible. I only mention this because in XCode you need to change your .m file to a .mm file for it to treat it this way, which tripped me up for a few minutes.
Example header:
void addMethod(NSInteger a);
void addMethod(NSInteger a, NSInteger b);
If you are including this in a .m file you'll get a warning:
Conflicting types for 'addMethod'
Changing your .m file into a .mm clears the issue.
回答6:
You can overload C functions with newer compilers - Syntax is a little clunky though (nothing a quick #define can't fix), but it works well.
function overloading in C
回答7:
Categories provide another way to emulate c-style method overloading in Objective C. As an example, consider the following interface declarations:
@interface MyClass
{
}
@end
@interface MyClass ( MethodVersion1 )
- (void) doSomething : (int) val;
@end
@interface MyClass ( MethodVersion2 )
- (void) doSomething : (double) val;
@end
The different parameter types are resolved by the Categories.