I am trying to call a function declared in ViewController class from DetailViewController class.
When trying to debug the \'Extra Argument in call\" error pops up.<
You have to call it like this:
ViewController.setCity(city, index: 5)
Swift has (as Objective-C) named parameters.
I have had this error when there is nothing at all wrong with the expression highlighted by the compiler and nothing wrong with the arguments specified, but there is an error on a completely different line somehow linked to the original one. For example: initialising object (a) with objects (b) and (c), themselves initialised with (d) and (e) respectively. The compiler says extra argument on (b), but in fact the error is a type mismatch between the type of (e) and the expected argument to (c).
So, basically, check the whole expression. If necessary, decompose it assigning the parts to temporary variables.
And wait for Apple to fix it.
In some cases, "Extra argument in call" is given even if the call looks right, if the types of the arguments don't match that of the function declaration. From your question, it looks like you're trying to call an instance method as a class method, which I've found to be one of those cases. For example, this code gives the exact same error:
class Foo {
func name(a:Int, b: Int) -> String {
return ""
}
}
class Bar : Foo {
init() {
super.init()
Foo.name(1, b: 2)
}
}
You can solve this in your code by changing your declaration of setCity to be class func setCity(...)
(mentioned in the comments); this will allow the ViewController.setCity
call to work as expected, but I'm guessing that you want setCity
to be an instance method since it appears to modify instance state. You probably want to get an instance to your ViewController class and use that to call the setCity method. Illustrated using the code example above, we can change Bar as such:
class Bar : Foo {
init() {
super.init()
let foo = Foo()
foo.name(1, b: 2)
}
}
Voila, no more error.
This error will ensue, if there is a conflict between a class/struct method, and a global method with same name but different arguments. For instance, the following code will generate this error:
You might want to check if there is such conflict for your setCity method.
In my case calling non-static function from static function caused this error. Changing function to static fixed the error.
In latest Swift 2.2, I had a similar error thrown which took me a while to sort out the silly mistake
class Cat {
var color: String
var age: Int
init (color: String, age: Int) {
self.color = color
self.age = age
}
convenience init (color: String) {
self.init(color: color, age: 1){ //Here is where the error was "Extra argument 'age' in call
}
}
}
var RedCat = Cat(color: "red")
print("RedCat is \(RedCat.color) and \(RedCat.age) year(s) old!")
The fix was rather simple, just removing the additional '{ }' after 'self.init(color: color, age: 1)' did the trick, i.e
convenience init (color: String) {
self.init(color: color, age: 1)
}
which ultimately gives the below output
"RedCat is red and 1 year(s) old!"