具有关联值的 Swift 枚举,在调用中得到 "missing argument label ' 数组”

标签 swift enums

我正在学习 Swift。下面是我的代码:

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)")
} 

当我运行它时,我得到了

missing argument label 'array' in call. 

错误来自:

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

如何修复错误?

最佳答案

通过插入数组:标签:

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

因为函数的定义需要它:

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

或者,如果您将 GiveMeFunc 重新定义为没有命名参数,则您不必提供它:

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

关于具有关联值的 Swift 枚举,在调用中得到 "missing argument label ' 数组”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29475501/

相关文章:

ios - 通话中的额外错误? Core Data 应用程序的 Swift 2 问题

ios - 在 Swift 中使用 block 给出错误 "Variable used within its own initial value"

iOS 通用链接 : Tapping universal link inside the Application redirects me to safari instead of continuing in the application

java - 为什么 JNI 在枚举中找不到自定义静态?

c# - 枚举需要索引吗?

ios - Realm 浏览器不允许我打开文件

arrays - 我应该如何获取作为 Int 通过测试的对象的最后一个索引?

c# - 为什么枚举权限经常有 0、1、2、4 值?

Java 枚举 - 返回字符串

c# - 当从 C# 中的动态获取属性时,如何检查枚举属性?