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

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"))

Related

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

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.

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.

Error: value of type 'String' has no member 'Generator' in Swift [duplicate]

This question already has answers here:
Iterate through a String Swift 2.0
(4 answers)
Closed 7 years ago.
I have this piece of code in Swift:
var password = "Meet me in St. Louis"
for character in password {
if character == "e" {
print("found an e!")
} else {
}
}
which throws the following error: value of type 'String' has no member 'Generator' in Swift in line: for character in password
I have tried to find online the possible error, but I can't (plus I am new to Swift and trying to navigate myself through the idiosyncrasies of the language).
Any help would be much appreciated (plus a brief explanation of what I am missing if possible)
In order for your code to work you need to do this :
var password = "Meet me in St. Louis"
for character in password.characters {
if character == "e" {
print("found an e!")
} else {
}
}
The problem with your code not working was the fact that it was not iterating over your array looking for a particular Character. Changing it to password.characters forces i to "search" each Character of the array password and voila.This behaviour happens in swift 2.0 because String no longer conforms to SequenceType protocol while String.CharacterView does!

Why does SWIFT print "Optional(...) [duplicate]

This question already has answers here:
println dictionary has "Optional"
(2 answers)
Closed 7 years ago.
If the following code runs
var airports = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
print(airports["YYZ"])
Why does the console print
Optional("Toronto Pearson")
Why does it print Optional( withValue ) and not just the value?
Why would I need to know that in the console?
Swift has optional types for operations that may fail. An array index like airports["XYZ"] is an example of this. It will fail if the index is not found. This is in lieu of a nil type or exception.
The simplest way to unwrap an optional type is to use an exclamation point, like so: airports["XYZ"]!. This will cause a panic if the value is nil.
Here's some further reading.
You can chain methods on option types in Swift, which will early exit to a nil without calling a method if the lefthand value is nil. It works when you insert a question mark between the value and method like this: airports["XYZ"]?.Method(). Because the value is nil, Method() is never called. This allows you to delay the decision about whether to deal with an optional type, and can clean up your code a bit.
To safely use an optional type without panicking, just provide an alternate path using an if statement.
if let x:String? = airports["XYZ"] {
println(x!)
} else {
println("airport not found")
}