Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 first parameter names

Tags:

In Swift 2, it appears that the first parameter name is not always required when calling a function. Now in Swift 3, the first parameter name is required when calling the function. For example:

func frobnicate(runcible: String) { 
    print("Frobnicate: \(runcible)") 
}

Swift 2.2 allowed the function to be called by simply typing:

Frobnicate("Station")

Swift 3 seems to be requiring that we use the first parameter names of methods such as:

Frobnicate(runcible:"Station")

Is this the case with Swift 3 for all functions and methods or just certain situations?

like image 437
Laurence Wingo Avatar asked Jul 13 '16 05:07

Laurence Wingo


People also ask

What are argument labels?

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.

What is Inout parameter in Swift?

Swift inout parameter is a parameter that can be changed inside the function where it's passed into. To accept inout parameters, use the inout keyword in front of an argument. To pass a variable as an inout parameter, use the & operator in front of the parameter.

What is #function in Swift?

Function Definition In Swift 4, a function is defined by the "func" keyword. When a function is newly defined, it may take one or several values as input 'parameters' to the function and it will process the functions in the main body and pass back the values to the functions as output 'return types'.

What is parameter in Swift?

A function parameter is a value that is accepted by a function. Before you learn about function parameters and return values, make sure to know about Swift functions. Let's see an example, func addNumbers(a: Int, b: Int) { var sum = a + b print("Sum:", sum) } addNumbers(a: 2, b: 3)

What is a function parameter in Swift?

A function parameter is a value that is accepted by a function. Before you learn about function parameters and return values, make sure to know about Swift functions. In the above example, the function addNumbers () takes two parameters: a and b. Notice the line,

How do you call a function in Swift?

You give a function a name that identifies what it does, and this name is used to “call” the function to perform its task when needed. Swift’s unified function syntax is flexible enough to express anything from a simple C-style function with no parameter names to a complex Objective-C-style method with names and argument labels for each parameter.

What is an in-out parameter in Swift?

Parameters can provide default values to simplify function calls and can be passed as in-out parameters, which modify a passed variable once the function has completed its execution. Every function in Swift has a type, consisting of the function’s parameter types and return type.

What is Swift’s unified function syntax?

Swift’s unified function syntax is flexible enough to express anything from a simple C-style function with no parameter names to a complex Objective-C-style method with names and argument labels for each parameter.


2 Answers

Yes, this is right. Swift is fixing a language inconsistency this way (this was always required for initializers).

If you don't want to use the external parameter name, just remove it explicitly:

func frobnicate(_ runcible: String) { 
    print("Frobnicate: \(runcible)") 
}

You can read the full rationale in Swift Evolution 0046

like image 152
Sulthan Avatar answered Sep 22 '22 16:09

Sulthan


You can read The Swift Programming Language (Swift 3) in i-Book. Also you can check this out in WWDC 2016: What's New in Swift

In Swift 3, by default, functions use their parameter names as labels for their arguments. Write a custom argument label before the parameter name, or write _ to use no argument label.

fun greet(_ person: String, on day: String) -> String {
    return "Hello \(person), today is \(day)."
}
greet("John", on: "Wednesday")

or

// This also works with Swift 2
fun addThreeNumber(_ first: Int, _ second: Int, _ third: Int) {
    print(first+second+third)
}
addThreeNumber(1, 2, 3)
like image 38
Willjay Avatar answered Sep 20 '22 16:09

Willjay