0

Does somebody have an idea on why this code doesn't compile with Xcode 6 beta 7? It must be a very stupid mistake, or compiler's bug:

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

I have tried using a Dictionary [State: Int] instead of Array [Int], and copiler gives also error. Thanks!

1 Answer 1

3

It looks like you forgot the return type in your getResult function:

func getResult(#state: State) -> Int {
Sign up to request clarification or add additional context in comments.

3 Comments

Yeah... then the "stupid mistake" option wins. But Xcode doesn't seem to assess very well what is going on, though. The compiler should know that a function with no return type shouldn't try to return anything, right? Throwing "State not convertible to Int" is somehow misleading...
... as almost any of the compiler error messages - when it doesn't make sense, usually it's a simple missing thing, or about incorrect usage of optionals
There's quite a few confusing or fairly generic error messages related to Swift code. I'm hoping that those will be cleaned up and improved by the time it's out of beta.

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.