1

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?

1
  • @MaximShoustin look in the case Commented Apr 6, 2015 at 16:55

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Oh I don't know in f(array: 1, 2, 4, 3) 1, 2, 4, 3 are considered as an array. I've tried f(array: [1, 2, 4, 3]) but it does not work. Interesting. Thank you!
Int... is what's called a variadic parameter. To the caller, it looks like supplying multiple separate arguments, but inside the function itself those are all presented as an array.
2

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.