"of" before a function argument in swift 3 [duplicate] - swift

This question already has answers here:
What are the new "for", "at", "in" keywords in Swift3 function declarations?
(1 answer)
What is the difference between didMove(to view: SKView) and didMoveToView(view: SKView)?
(2 answers)
Closed 5 years ago.
What is the usage of the word "of" before a function argument in swift 3?

This is an argument label:
func someFunction(argumentLabel parameterName: Int) {
// In the function body, parameterName refers to the argument value
// for that parameter.
}
Check the documentation on function declaration for more information:
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

How can I pass an array as parameter without using Brackets in Swift? [duplicate]

This question already has answers here:
passing unknown number of arguments in a function
(3 answers)
Closed 1 year ago.
I want to know how could I pass an array as parameter to function without using Brackets, like printin Swift. as you can see in down it can be 3 or 5 or 7 strings, it would work
print("1", "2", "3")
func customFunction(???) { }
you can achieve this via Variadic functions this allows you to input parameter as comma separated values. just put 3 dots at the the end of parameter type
func customFunction(values: String...) {
for value in values {
print(value)
}
}
function call
customFunction(values: "1", "2", "3")

What is the keyword repeating used for in Swift? [duplicate]

This question already has answers here:
What is the point of having two different names for the same parameter?
(3 answers)
What are the new "for", "at", "in" keywords in Swift3 function declarations?
(1 answer)
Closed 4 years ago.
I am reading the official "The Swift Programming Language (Swift 4.2)" book and in the part Swift Tour/Generics I encounter the following code
func makeArray1<Item>(repeating item: Item, numberOfTimes: Int) -> [Item] {
var result = [Item]()
for _ in 0..<numberOfTimes {
result.append(item)
}
return result
}
let arr1 = makeArray1(repeating: "knock", numberOfTimes: 4)
print(arr1)
which prints out
["knock", "knock", "knock", "knock"]
I am confused with the role of the word 'repeating' in the definition of the function makeArray1. I tried to run the code without that word with the following code
func makeArray2<Item>(item: Item, numberOfTimes: Int) -> [Item] {
var result = [Item]()
for _ in 0..<numberOfTimes {
result.append(item)
}
return result
}
let arr2 = makeArray2(item:"knock", numberOfTimes:4)
print (arr2)
and the code gave the same result as before.
["knock", "knock", "knock", "knock"]
So what is the use of 'repeating' in the code?
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.

Getting "missing argument label" error trying to call Swift method [duplicate]

This question already has answers here:
Swift 3 first parameter names
(5 answers)
Closed 5 years ago.
I'm new to Swift. I have a problem with my simple function. It is not working and Xcode playground gives an error:
missing argument label 'name:' in call print(hello("txt"))
Here is the code:
func hello(name:String)->String{
return name
}
print(hello("txt"))
How can I fix this to have a working function?
You Missing argument label 'name:' in call print(hello("txt"))
Try this way
func hello(name:String)->String{
return name
}
print(hello(name : "txt"))
You should read the language guide.
https://developer.apple.com/.../Swift.../TheBasics.html
You are missing an argument label that's the reason:
You can call like below
print(hello(name : "txt"))
OR you can ignore argument with prefix "_".
func hello(_ name:String)->String{
return name
}
print(hello("txt"))

What does 'using' do in Swift methods? [duplicate]

This question already has an answer here:
What are the new "for", "at", "in" keywords in Swift3 function declarations?
(1 answer)
Closed 5 years ago.
I noticed that in I was getting an error unless I did
animateTransition(using transitionContext: UIViewControllerContextTransitioning)
However some tutorial present this method as,
animateTransition(transitionContext: UIViewControllerContextTransitioning) without the using.
It only seems to build if I include using but I'm curious as to it's role and when the change occurred.
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.

Why can I call Array.max this way? [duplicate]

This question already has an answer here:
Parameters after opening bracket
(1 answer)
Closed 6 years ago.
I'm using Xcode 8.2.1. If I look at the documentation for Array I find this declaration for the max method:
public func max(by areInIncreasingOrder: (Element, Element) throws -> Bool) rethrows -> Element?
The argument label is by and the argument name is areInIncreasingOrder. Since the label is specified explicitly I thought it has to be included in a call to the function but the following code works if I omit the label (i.e by).
Am I misunderstanding how argument labels are used when calling a method? Or, is my example code calling a different version of the max method?
Example code:
let names = ["Talyor", "Paul", "Adele"]
let longest = names.max { $1.characters.count > $0.characters.count }
print(longest!) // "Taylor
When the last parameter of a method is a closure, you can write it in curly braces after the method call and omit the name of the parameter.
See the Trailing Closure documentation.