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

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 ????
    }
}

我试过用字典[State:Int]代替数组[Int],复印机也会出错。谢谢!

最佳答案

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

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

08-05 23:53