Just a learner
Just a learner

Reputation: 28622

Swift enum that has associated values, got "missing argument label 'array' in call"

I'm learning Swift. Below is my code:

enum MyTest
{
    case Point(Int, Int)
    case What(String)
    case GiveMeFunc((array: Int...) -> Int)
}

var p1 = MyTest.Point(2, 2)
var p2 = MyTest.Point(3, 3)

var s1 = MyTest.What("Haha...")
var s2 = MyTest.What("Heihei...")

var f1 = MyTest.GiveMeFunc({(array: Int...) -> Int in return 8})
var f2 = MyTest.GiveMeFunc({(array: Int...) -> Int in return 9})

switch p1 {
    case .What(let string):
        println(string)
    case .GiveMeFunc(let f):
        println(f(1, 2, 4, 3))
    case .Point(let a, let b):
        println("a: \(a)  b: \(b)")
} 

When I run it I got

missing argument label 'array' in call. 

The error comes from :

println(f(1, 2, 4, 3))` 

How to fix the error?

Upvotes: 1

Views: 539

Answers (2)

Arbitur
Arbitur

Reputation: 39091

As you can see in the error message: missing argument label 'array' in call

If you look at your closure GiveMeFunc((array: Int...) -> Int) it takes a parameter called array

So the error message is saying that you're missing that parametername

So by doing: println(f(array: 1, 2, 4, 3)) it works

Or changing the closure to GiveMeFunc((Int...) -> Int)

Upvotes: 2

Airspeed Velocity
Airspeed Velocity

Reputation: 40963

By inserting the array: label:

case .GiveMeFunc(let f):
    println(f(array: 1, 2, 4, 3))

since the definition of the function requires it:

case GiveMeFunc((array: Int...) -> Int)

Alternatively if you redefined GiveMeFunc to not have a named argument, you wouldn't have to supply it:

case GiveMeFunc((Int...) -> Int)

Upvotes: 3

Related Questions