Swift : missing argument label 'xxx' in call
One possible reason is that it is actually a method. Methods are very sneaky, they look just like regular functions, but they don't act the same way, let's look at this:
func funFunction(someArg: Int, someOtherArg: Int) {
println("funFunction: \(someArg) : \(someOtherArg)")
}
// No external parameter
funFunction(1, 4)
func externalParamFunction(externalOne internalOne: Int, externalTwo internalTwo: Int) {
println("externalParamFunction: \(internalOne) : \(internalTwo)")
}
// Requires external parameters
externalParamFunction(externalOne: 1, externalTwo: 4)
func externalInternalShared(#paramOne: Int, #paramTwo: Int) {
println("externalInternalShared: \(paramOne) : \(paramTwo)")
}
// The '#' basically says, you want your internal and external names to be the same
// Note that there's been an update in Swift 2 and the above function would have to be written as:
func externalInternalShared(paramOne paramOne: Int, #paramTwo: Int) {
print("externalInternalShared: \(paramOne) : \(paramTwo)")
}
externalInternalShared(paramOne: 1, paramTwo: 4)
Now here's the fun part, declare a function inside of a class and it's no longer a function ... it's a method
class SomeClass {
func someClassFunctionWithParamOne(paramOne: Int, paramTwo: Int) {
println("someClassFunction: \(paramOne) : \(paramTwo)")
}
}
var someInstance = SomeClass()
someInstance.someClassFunctionWithParamOne(1, paramTwo: 4)
This is part of the design of behavior for methods
Apple Docs:
Specifically, Swift gives the first parameter name in a method a local parameter name by default, and gives the second and subsequent parameter names both local and external parameter names by default. This convention matches the typical naming and calling convention you will be familiar with from writing Objective-C methods, and makes for expressive method calls without the need to qualify your parameter names.
Notice the autocomplete:
Getting missing argument label error trying to call Swift method
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
Swift error: missing argument label 'name:' in call
Swift functions can specify local and external argument names:
func greet(who name: String = "world") {
println("hello \(name)")
}
// prints "hello world"
greet()
// prints "hello jiaaro"
greet(who:"jiaaro")
// error
greet("jiaaro")
// error
greet(name: "jiaaro")
To opt out of this behavior you can use an underscore for the external name. Note that the first parameter implicitly uses the "no external name" behavior:
func greet(name: String = "world", _ hello: String = "hello") {
println("\(hello) \(name)")
}
// prints "hello world"
greet()
// prints "hello jiaaro"
greet("jiaaro")
// prints "hi jiaaro"
greet("jiaaro", "hi")
// error
greet(name: "jiaaro")
The following is now disallowed in Swift 2.0, see below for equivalent code.
You can use the #
prefix to use the same local and external name for the first parameter:
func greet(#name: String = "world", hello: String = "hello") {
println("\(hello) \(name)")
}
// prints "hi jiaaro"
greet(name: "jiaaro", hello: "hi")
Swift 2.0 code:
func greet(name name: String = "world", hello: String = "hello") {
println("\(hello) \(name)")
}
// prints "hi jiaaro"
greet(name: "jiaaro", hello: "hi")
xcode: Missing argument label 'at:' in call
There is no remove
method on arrays that takes the thing you want to remove as a parameter, you can only remove the element at a certain index:
self.comments.remove(at: self.comments.index(of: post["message"] as! String)!)
If you want such a remove
function, consider using a Set
instead, or write your own extension:
extension Array where Element : Equatable {
@discardableResult
mutating func remove(element: Element) -> Element? {
guard let i = self.index(of: element) else { return nil }
return self.remove(at: i)
}
}
Swift Contains Array Error: Missing argument label 'where:' in call
The error is a bit misleading. array(forKey
returns [Any]?
so you have to cast the object to the expected type to ensure that the type conforms to Equatable
if let items = UserDefaults.standard.array(forKey: "purchasedItems") as? [Int] {
if items.contains(1) {
print("works!")
}
}
Related Topics
How to Make a Https Request to a Server in Swift
How to Animate Transition Between Views in Swiftui
How to Change Colour of the Certain Words in Label - Swift 3
Result Values in '? :' Expression Have Mismatching Types 'Some View' and '...'
Swift Lazy Subscript Ignores Filter
Adding a Search Bar to Navigationview in Swiftui
Select All Text in Textfield Upon Click Swiftui
How to Provide Default Implementations for Uipageviewcontrollerdatasource
Swift: Unable to Decompose Tuple in Certain Closures (E.G., Reduce with Enumerate)
Convert Timestamp String with Epochal Time and Timezone into Nsdate
How to Detect a 'Click' Gesture in Swiftui Tvos
How to Create String Split Extension with Regex in Swift
Get Header Data from a Request Response in Swift
Navigationview Doesn't Display Correctly When Using Tabview in Swiftui
Convert Emoji to Hex Value Using Swift