Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do _:_: and similar combinations of the colon and underscore mean in Swift? [duplicate]

Tags:

oop

ios

swift

When reading Swift's documentation, Apple commonly uses functionName(_:name:) or something similar. What exactly is this pattern that sometimes is _:_:, sometimes just _:, and _:name:. I think it has to do with parameter shorthand, but I'm not sure, and can't find the explanation in Swift's programming guide. Thank you!

Example:

insert(_:atIndex:)
like image 605
rb612 Avatar asked Jun 25 '15 02:06

rb612


People also ask

What does the _ mean in a Swift function?

In Swift, the underscore operator (_) represents an unnamed parameter/label. In for loops, using the underscore looping parameter is practical if you do not need the looping parameter in the loop.

What does _: mean in Swift documentation?

In the documentation it is being used as a wildcard to indicate a function that takes an unnamed parameter.

What is @available in Swift?

When navigating through Swift APIs you'll often run into the @available attribute. We've just covered the #attribute which is similar but just a bit different. The shortest answer to describe its difference: @available is used to mark the availability for a class or method.

What is argument label in Swift?

The argument label is used when calling the function; each argument is written in the function call with its argument label before it. The parameter name is used in the implementation of the function. By default, parameters use their parameter name as their argument label.


2 Answers

The underscore indicates that there is no external parameter name for the function. Apple's Swift Documentation talks about this concept in terms of when you're writing your own functions.

Take the case where you write this function (from the documentation):

func sayHello(to person: String, and anotherPerson: String) -> String { ... }

If you were to use the function you would write the following:

sayHello(to: "Bill", and: "Ted")

The signature for this would be sayHello(to:and:). However, what if we wanted to use the function as sayHello("Bill", "Ted")? How would we indicate that we didn't want an external parameter name? Well that's where the underscore comes in. We could rewrite the function as:

func sayHello(person: String, _ anotherPerson: String) -> String { ... }

Note the first parameter doesn't need the _, but subsequent ones will. The first is inferred to have no parameter name. This makes the method signature for this call sayHello(_:_:) because you as the caller don't have a named parameter.

Update Swift 3.0:

Swift 3.0 treats all parameters equally. The first parameter now requires an underscore to indicate the absense of an external parameter name. In the above example of having sayHello("Bill", "Ted") at the call site, your corresponding function or method declaration would have to be

func sayHello(_ person: String, _ anotherPerson: String) -> String { ... }

Note the addition of the underscore before the internal parameter name 'person'.

like image 171
Lucas Derraugh Avatar answered Nov 15 '22 16:11

Lucas Derraugh


The underscore indicates an ignored value. You can read more about it here: What's the _ underscore representative of in Swift References?

In the documentation it is being used as a wildcard to indicate a function that takes an unnamed parameter.

like image 27
Subcreation Avatar answered Nov 15 '22 15:11

Subcreation