Search code examples
iosswiftxcodecode-hinting

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


Is there any rule when Xcode displays hint of the function/method with default parameter value?

Here's what I mean. For example, in UIViewController there's present() method with the signature like this:

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

When I type its name in Xcode, I'm only hinted of the full method declaration, including completion parameter, which can be omitted:

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

However, let's create present method's copy:

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

This time, Xcode informs about two possible versions of the method:

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

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

Where does this inconsistency come from?


Solution

  • I guess this is because you are defining the method in a pure Swift class whereas present method of UIViewController is defined in Objective-C class.

    In Objective-C there is no concept of default parameter values.

    Even the opposite is true. If you access your presentCopy method inside an Obj-C Class, the method declaration will have all the values and there will be only one method.