使用下标扩展数组(Swift)。 Xcode 6 beta 7中的错误?

时间:2014-09-05 14:50:49

标签: arrays xcode swift

有人知道为什么这段代码不能用Xcode 6 beta 7编译吗?它一定是一个非常愚蠢的错误,或编译器的错误:

enum State : UInt8 {
    case Off = 0
    case On  = 1
}

extension Array {
    subscript (index: State) -> Element {
        get { 
            let i = Int(index.toRaw())
            return self[i] 
        }
        set { 
            let i = Int(index.toRaw())
            self[i] = newValue 
        }
    }
}

class MyClass  {
    var results = [0, 7]
    func getResult(#state: State) { 
        return results[state]  // Error here: State not convertible to Int ????
    }
}

我尝试使用Dictionary [State:Int]而不是Array [Int],并且copiler也给出了错误。谢谢!

1 个答案:

答案 0 :(得分:3)

您似乎忘记了getResult函数中的返回类型:

func getResult(#state: State) -> Int {