Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument labels before parameters in functions - possible bug in Swift Markup?

I have the following function in a class:

/// Returns the weather conditions at the given location.
/// - parameter for: A location on the Earth's surface.
/// - returns: If found, the `WeatherConditions` at the supplied location otherwise nil.
public func conditions(for location: Location) -> WeatherConditions? {
    return nil  // The actual code is not important to the question.
}

which is called as follows let myWeather = conditions(for: myLocation).

The code works fine, the question is about the documentation. The image below is what is seen in the 'quick help' window for the conditions function. Given that the user of the function must use the external argument label (for) and also that I have explicitly documented that label, shouldn't the parameters line in the quick help window read Parameters for and not Parameters location?

Is this a bug in Xcode or is there a reason the (internal) parameter name is displayed and not the external argument label?

enter image description here

like image 512
Vince O'Sullivan Avatar asked Aug 19 '16 07:08

Vince O'Sullivan


People also ask

How do you omit an argument label in Swift?

To omit argument labels, use a _ before the parameter name. A parameter name is still necessary to access the argument's value inside the function. Omitting the argument label might be wanted to make the function more readable.

What is an 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.

How do you pass a function as an argument in Swift?

If a function takes another function as an argument in Swift, you do not need to specify a separate function and pass that as an argument. Instead, you can use a closure function. A closure is an anonymous function. It is a block of code that acts as a function but does not have a name.

What is Variadic parameters in Swift?

In Swift, variadic parameters are the special type of parameters available in the function. It is used to accept zero or more values of the same type in the function. It is also used when the input value of the parameter is varied at the time when the function is called.


1 Answers

Quite simply, for is not the parameter; location is. And quick help is documenting parameters.

Preferring that quick help identify for as a location on the Earth's surface, as has been suggested, would be a bit puzzling to the reader.

The SPLG and API Design Guidelines use the terminology "parameter" and "argument label" (as in your question title) instead of "internal parameter" and "external parameter" (which may lead to the confusion you're raising). Given this, parameters fall under the Parameters heading in quick help, and argument labels appear in the Declaration.

like image 157
Kevin Owens Avatar answered Nov 27 '22 00:11

Kevin Owens