What Are the New "For", "At", "In" Keywords in Swift3 Function Declarations

What are the new for, at, in keywords in Swift3 function declarations?

The syntax allow you to label arguments twice - once for use within the function (the parameter name) and once for use when calling (the argument label). By default these two will be the same (like with sender in your example), but they differ when it makes a function more readable at the call site:

prepare(for: aSegue, sender: something)

being more readable-as-a-sentence than:

prepare(segue: aSegue, sender: something)

Which sounds like you're preparing the segue, not preparing for the segue.

for would be a dreadful name to use internally in the function to refer to the segue, so you can use a different one if you require.

The idea is to meet the sometimes conflicting goals of readability at the call site and sensible naming in the implementation.

When defining a function, if you are going to use separate argument labels and parameter names, you specify the argument label first, and the parameter name second:

func sayHello(to name: String) {
print("Hello " + name)
}

sayHello(to: "Pekka")

to only has relevance when calling the function. name is used inside the function.

What is the purpose of for:, with: in function parameters?

Mainly it is just for the readability. You can actually read what the code does: configure text for (the cell) with (this item/data)

Swift: Function-argument with for

for is an argument label.

Each function parameter has both an argument label and a parameter name. The argument label is used when calling the function; each argument is written in the function call with its argument label before it.

Argument labels can help with readability by allowing you to use a label that makes sense when calling a function and a different parameter name in the function body.

Consider the function in your question. The caller writes

someVC.prepare(for: someSegue, sender:sender)

This reads somewhat like a sentence "prepare for some segue".

In the implementation of the function you can use the more meaningful segue rather than the less meaningful for.

Use of in in a function's arguments

Functions in Swift support both an external and an internal name. This effectively means you can refer to the same parameter using two different names. That's what in is in the code you provided, an external parameter name.

The difference between external and internal parameters is that you use the first when calling a method, and the second when dealing with the value inside such method's body.

So, you would call the method as such:

// Call the method using the external parameter name in
pickUpUser(in: someRideOptionID)

func pickUpUser(in rideOptionID: RideOptionID) {
// Do something with the value, accessing it using the internal parameter name rideOptionID
}

What is the keyword repeating used for in Swift?

It's not a keyword, it's an optional function argument label that can differ from the local parameter name that's used inside the function/method.

Read the section Function Argument Labels and Parameter Names in The Swift Programming Language:

Each function parameter has both an argument label and a parameter name. 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. …

You write an argument label before the parameter name, separated by a space …

If you don’t want an argument label for a parameter, write an underscore (_) instead of an explicit argument label for that parameter.

What does 'on' mean in Swift?

That's the "external name" for the parameter. So within the function you refer to the argument as day, but when calling it, you use the label on. It's not a Swift keyword.

Swift for inside of ( ) what is this..?

This is a simple parameter name. You can change that to anything you like so it appears in the method call. You could also leave it out completely. Then, the method call would be:

getIndex(priority: ...)

instead of:

getIndex(for: ...)

The $0 is from the .firstIndex method. If you don't define a parameter name, then $0 allows you to access the value anyway.

You could also do:

prioritizedTasks.firstIndex { task in
task.priority == priority
}!

What does 'using' do in Swift methods?

check apple documentation

Specifying Argument Labels


You write an argument label before the parameter name, separated by a
space:

func someFunction(argumentLabel parameterName: Int) {
// In the function body, parameterName refers to the argument value
// for that parameter.
}

Here’s a variation of the greet(person:) function that takes a
person’s name and hometown and returns a greeting:

func greet(person: String, from hometown: String) -> String {
return "Hello \(person)! Glad you could visit from \(hometown)."
}
print(greet(person: "Bill", from: "Cupertino"))
// Prints "Hello Bill! Glad you could visit from Cupertino."

The use of argument labels can allow a function to be called in an
expressive, sentence-like manner, while still providing a function
body that is readable and clear in intent.



Related Topics



Leave a reply



Submit