2

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?

Robo Robok
  • 21,132
  • 17
  • 68
  • 126
  • they are internal rules I don't think you can alter with. what is the reason you are asking? And where is the incosistency exactly? – Mohamad Bachir Sidani Aug 08 '17 at 12:31
  • The reason I'm asking is because I'd like to know when I can omit a parameter and when not, without looking up every method in the documentation. Inconsistency lays in the fact that is sometimes gives you all possible shapes of a method, and sometimes just the fullest. – Robo Robok Aug 08 '17 at 12:33
  • As far as I know this will only happen in swift and nt objective c. maybe because it is built on top of it – Mohamad Bachir Sidani Aug 08 '17 at 12:34

1 Answers1

4

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.

Puneet Sharma
  • 9,369
  • 1
  • 27
  • 33