为什么Xcode始终不显示参数默认值的提示?

时间:2017-08-08 12:24:19

标签: ios swift xcode code-hinting

当Xcode显示带有默认参数值的函数/方法的提示时,是否有任何规则?

这就是我的意思。例如,在UIViewController present()方法中,签名如下:

func present(
    _ viewControllerToPresent: UIViewController,
    animated flag: Bool,
    completion: (() -> Void)? = nil
)

当我在Xcode中输入其名称时,我只暗示了完整的方法声明,包括completion参数,可以省略:

  1.   

    present(viewControllerToPresent:UIViewController,animated:Bool,completion:(() - > Void)?)

  2. 但是,让我们创建present方法的副本:

    func presentCopy(
        _ viewControllerToPresent: UIViewController,
        animated flag: Bool,
        completion: (() -> Void)? = nil
    ) {...}
    

    这一次,Xcode告知了该方法的两个可能版本:

    1.   

      presentCopy(viewControllerToPresent:UIViewController,animated:Bool)

    2.   

      presentCopy(viewControllerToPresent:UIViewController,动画:Bool,   完成:(() - > Void)?)

    3. 这种不一致来自哪里?

1 个答案:

答案 0 :(得分:4)

我想这是因为您在纯Swift类中定义方法,而present的{​​{1}}方法在Objective-C类中定义。

在Objective-C中,没有默认参数值的概念。

即使相反也是如此。如果在Obj-C类中访问UIViewController方法,方法声明将包含所有值,并且只有一个方法。

相关问题