Fabrizio Bartolomucci
Fabrizio Bartolomucci

Reputation: 4948

'Extra Argument in Call' in completion call in Swift

I am trying to return a simple tuple in the completion of a function as in the following code:

func meditatorForId(id:Int, completion:((Meditator, Int)? -> Void)){
    var counter:Int=0
    for meditator in SanghaModelProvider.sharedProvider().meditatorsArray{
        if meditator.id == id{
            completion(meditator, counter)
            return
        }
        counter+=1
    }
    completion(nil)
}

Yet I get 'Extra Argument in Call' on the completion call line without getting more explanations. What could be the problem?

Upvotes: 0

Views: 927

Answers (1)

markwatsonatx
markwatsonatx

Reputation: 3491

You are trying to call completion with two arguments, but it should be a single argument with the tuple:

completion((meditator, counter))

i.e.:

let tuple = (meditator, counter)
completion(tuple)

If you would prefer to call it with two parameters then change your function to this:

func meditatorForId(id:Int, completion:((Meditator?, Int?) -> Void)) {

Then this will work:

completion(meditator, counter)

but you will have to change the 2nd call to:

completion(nil, nil)

Upvotes: 1

Related Questions