Objective-C中的方法重载? [英] Method overloading in Objective-C?
问题描述
据我所知,Objective-C不支持方法重载。在Objective-C中可以替代什么?或者我应该总是使用不同的方法名称?
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?
推荐答案
正确,客观-C不支持方法重载,所以你必须使用不同的方法名称。
Correct, objective-C does not support method overloading, so you have to use different method names.
注意,方法名称包括方法签名关键字(参数
名称位于:之前) ),所以以下是两个不同的方法,即使它们都开始writeToFile:
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;
(两个方法的名称是writeToFile:fromInt:和writeToFile:fromString: )。
(the names of the two methods are "writeToFile:fromInt:" and "writeToFile:fromString:").
这篇关于Objective-C中的方法重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!