0

I'm reading some chapters of the Apple's Swift Programming Language and I find that methods and functions signatures in the reference have an _ character as argument(s), for instance the print function:

print(_:separator:terminator:)

However, I don't find what this character exactly means.

Thanks in advance

AppsDev
  • 12,319
  • 23
  • 93
  • 186

2 Answers2

3

This is the syntax that allows callers to omit the external parameter name:

Omitting External Parameter Names

If you do not want to use an external name for the second or subsequent parameters of a function, write an underscore (_) instead of an explicit external name for that parameter.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

See this section in the documentation:

Omitting External Parameter Names

If you do not want to use an external name for the second or subsequent parameters of a function, write an underscore (_) instead of an explicit external name for that parameter.

This character indicates that the identifier of the argument isn't required in the method call:

This method

func sayHello(personName: String, alreadyGreeted: Bool) -> String

is referred to as

sayHello(_:alreadyGreeted:)

and called with

sayHello("Tim", alreadyGreeted: true)

You can see that alreadyGreeted: must be included in the function call, but not personName.

IIRC, the first argument in any function call is always omitted when calling the function but written out in the declaration (obviously, otherwise there would be no way to access it).

Community
  • 1
  • 1
Arc676
  • 4,445
  • 3
  • 28
  • 44