为什么 Xcode 不总是显示参数默认值的提示?

Why doesn't Xcode always display a hint of parameter default value?

Xcode显示默认参数值function/method的提示有什么规律吗?

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

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

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

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

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

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

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

  1. presentCopy(viewControllerToPresent: UIViewController, animated: Bool)

  2. presentCopy(viewControllerToPresent: UIViewController, animated: Bool, completion: (() -> Void)?)

这种不一致从何而来?

我想这是因为您在纯 Swift class 中定义方法,而 UIViewControllerpresent 方法在 Objective-C class.

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

反之亦然。如果您在 Obj-C Class 中访问 presentCopy 方法,方法声明将包含所有值,并且只有一个方法。