我相信你问的是C++的默认参数化函数。
但是Objective-C不支持这个。
您可以创建,虽然2种方法:
-(void) changeButton:(UIButton *)button andAlpha:(float)alpha {
button.alpha = alpha;
}
-(void) changeButton:(UIButton *)button andAlpha:(float)alpha andEnabled:(BOOL)enabled {
button.alpha = alpha;
button.enabled = enabled;
}
对于C:有使用ObjC加入到C子集没什么特别的。任何无法在纯C中完成的事情都无法通过在ObjC中进行编译来完成。这意味着,您不能拥有默认参数,也不能重载一个函数。改为创建2个功能。
一种替代方式(位冗长,因为有人会很少使用)是有一个标志,并检查标志是/否。
-(void) changeButton:(UIButton *)button andAlpha:(float)alpha andEnabled:(BOOL)enabled withFlag:(BOOL)flag{
button.alpha = alpha;
if(flag){
button.enabled = enabled;
}
}
否。创建一个不同的方法,遗漏代码。 – trojanfoe