我知道有一种方法可以使用目标c中的方法获取可选值或默认值,但有没有办法用函数执行此操作?
尝试与方法相同的方法会产生冲突类型错误。
即..
void myfunc();
void myfunc(NSInteger myval);
答案 0 :(得分:0)
在Cocoa中没有任何东西叫做函数。
如果您正在使用C ++中的默认参数,那么Cocoa中没有任何内容。
但是,您可以使用多个方法共享具有不同参数的相同名称:
init //no arguments
initWithName: //one argument
initWithName:age: //two arguments
您可以将此C ++默认参数方法接近:
int sum(int a, int b=0, int c=0){
return a+b+c;
}
在objective-c中,但从不这样做:
<击> - (NSInteger)sumA:(NSInteger)a withB:(NSInteger)b withC:(NSInteger)c {
if(c==NIL) c=0;
if(b==NIL) b=0;
return a+b+c;
}
并将其命名为:[self sumA:10 withB:5 withC:nil];
<击>