75

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.

In ViewController class:

func setCity(item : Cities, index : Int)
{

    citiesArray!.removeObjectAtIndex(index)
    citiesArray!.insertObject(item, atIndex: index)
}

In detailViewController Class

 // city of type Cities
 ViewController.setCity(city ,5 ) //Error: "Extra argument in call" 

This is pretty simple yet I'm baffled.

Marin
  • 12,531
  • 17
  • 56
  • 80

9 Answers9

84

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.

Jesse Rosalia
  • 1,146
  • 7
  • 5
  • I had a very similar issue to this, where two of my objective-c methods have the same prefix, say update:... with different parameter lists after that. It was complaining with an impossible extra argument error, and after reading this answer I tried renaming the method it was complaining about to updateFoo: and that slight differentiation let the compiler produce the true error: that I had declared a parameter as the superclass of its true type. – binarymochi Oct 27 '14 at 15:04
  • Let me add that this can also be an issue when the method in question takes a C interoperability pointer type. – A. R. Younce Oct 31 '14 at 18:48
  • 1
    @binarymochi Thanks for the comment. I just ran into that exact issue, and used what you suggested to debug it. – Jesse Rosalia Nov 09 '14 at 23:04
  • 1
    For me, I had to explicitly convert swift types (String, Dictionary) to objective-C types (NSString, NSMutableDictionary). After changing that it detected the correct method. – Tiago Lira Nov 19 '14 at 12:54
  • 1
    I got this compiler error when I mistakenly had an extra `{` where I shouldn't have. Check your `try/catch` and your `if let`'s to make sure they're lined up. Hopefully this helps someone else – Eric Jun 29 '15 at 23:21
  • Swift 3 still does not provide a better error in some cases when calling instance methods as if they were static :/ – Eneko Alonso Aug 29 '16 at 03:22
  • This is a totally ridiculous problem with XCode. I was getting also this error message with a totally unrelated problem, and XCode was even pointing to a different argument. So in case you get this error and XCode points you to one specific argument, please check them all. – Azurlake Jan 25 '17 at 12:48
50

SwiftUI:

This error message "extra argument in call" is also shown, when all your code is correct, but the maximum number of views in a container is exceeded (in SwiftUI). The max = 10, so if you have some different TextViews, images and some Spacers() between them, you quickly can exceed this number.

I had this problem and solved it by "grouping" some of the views to a sub container "Group":

        VStack {
            
            Text("Congratulations")
                .font(.largeTitle)
                .fontWeight(.bold)
            
            Spacer()
            
            // This grouping solved the problem
            Group {
                Text("You mastered a maze with 6 rooms!")
                Text("You found all the 3 hidden items")
            }
            
            Spacer()

            // other views ...
             
        }
LukeSideWalker
  • 7,399
  • 2
  • 37
  • 45
12

In my case calling non-static function from static function caused this error. Changing function to static fixed the error.

ibrahimyilmaz
  • 2,317
  • 1
  • 24
  • 28
3

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:

enter image description here

You might want to check if there is such conflict for your setCity method.

Shripada
  • 6,296
  • 1
  • 30
  • 30
  • 1
    To explicitly call the global function, use the module name. For example `Swift.min(x,y)`. – Yonat Nov 25 '16 at 08:57
  • 2
    That is the most hideous screen background color I have ever seen. How do you program? – FontFamily May 14 '21 at 21:40
  • I have some vision problem @FontFamily. – Shripada Jun 17 '21 at 04:10
  • 1
    @Shripada No kidding - please educate me. I want to be more sensitive to things like this. It couldn't be red/green blindness or blue/yellow. What can you share? And apologies for me being a jerk. – FontFamily Jun 17 '21 at 19:58
2

You have to call it like this:

ViewController.setCity(city, index: 5)

Swift has (as Objective-C) named parameters.

dasdom
  • 13,975
  • 2
  • 47
  • 58
  • 4
    I get the same error. Only language in the world that makes an experienced programmer look dumb when he can't figure out how to call a function. – The Muffin Man Feb 01 '15 at 23:31
2

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.

rghome
  • 8,529
  • 8
  • 43
  • 62
2

This can also happen if you have more than 10 views for ViewBuilder arguments in SwiftUI

ScottyBlades
  • 12,189
  • 5
  • 77
  • 85
0

This is not the direct answer to this question but might help someone. In my case problem was that class with same name existed in a different (Test) target.

While running Test target I got this error as a new argument was added to init method but was missing in the class in Test target.

Adding the same argument also to other init solved the issue.

Haseeb Iqbal
  • 1,455
  • 13
  • 20
-3

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!"
Naishta
  • 11,885
  • 4
  • 72
  • 54