ios Objective-C 中的方法重载?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2286312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Method overloading in Objective-C?
提问by suse
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?
据我所知,Objective-C 不支持方法重载。在 Objective-C 中有什么替代方案?或者我应该总是使用不同的方法名称?
回答by David Gelhar
Correct, objective-C does not support method overloading, so you have to use different method names.
正确,Objective-C 不支持方法重载,因此您必须使用不同的方法名称。
Note, though, that the "method name" includes the method signature keywords (the parameter names that come before the ":"s), so the following are two differentmethods, even though they both begin "writeToFile":
但是请注意,“方法名称”包括方法签名关键字(“:”之前的参数名称),因此以下是两种不同的方法,即使它们都以“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:").
(这两个方法的名称是“writeToFile:fromInt:”和“writeToFile:fromString:”)。
回答by Valentin Radu
It may be worth mentioning that even if Objective-C doesn't support methodoverloading, Clang + LLVM does support functionoverloading 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) versionof the visitor design pattern)
值得一提的是,即使 Objective-C 不支持方法重载,Clang + LLVM 确实支持C 的函数重载。实施轻微砍死(违背封装)版本的的访问者设计模式)
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));
回答by Ziminji
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.
David 是正确的,Objective-C 不支持方法重载。从这个意义上说,它类似于 PHP。正如他还指出的那样,以他举例的方式定义两个或多个具有不同签名的方法是常见的做法。但是,也可以使用“id”类型创建一种方法。通过“id”类型,您可以将任何对象(以及使用 NSNumber 类的任何原语)发送到该方法,然后从该方法本身内部测试其类型并在必要时抛出适当的异常。尽管这确实对性能造成了轻微影响,但除非您正在处理大量数据,否则它很可能是微不足道的或无关紧要的。
- (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