当Xcode显示带有默认参数值的函数/方法的提示时,是否有任何规则?
这就是我的意思。例如,在UIViewController
present()
方法中,签名如下:
func present(
_ viewControllerToPresent: UIViewController,
animated flag: Bool,
completion: (() -> Void)? = nil
)
当我在Xcode中输入其名称时,我只暗示了完整的方法声明,包括completion
参数,可以省略:
present(viewControllerToPresent:UIViewController,animated:Bool,completion:(() - > Void)?)
但是,让我们创建present
方法的副本:
func presentCopy(
_ viewControllerToPresent: UIViewController,
animated flag: Bool,
completion: (() -> Void)? = nil
) {...}
这一次,Xcode告知了该方法的两个可能版本:
presentCopy(viewControllerToPresent:UIViewController,animated:Bool)
presentCopy(viewControllerToPresent:UIViewController,动画:Bool, 完成:(() - > Void)?)
这种不一致来自哪里?
答案 0 :(得分:4)
我想这是因为您在纯Swift类中定义方法,而present
的{{1}}方法在Objective-C类中定义。
在Objective-C中,没有默认参数值的概念。
即使相反也是如此。如果在Obj-C类中访问UIViewController
方法,方法声明将包含所有值,并且只有一个方法。