user965972
user965972

Reputation: 2587

nested enum as dictionary key

Is there a reason why this doesn't work? Note: an enum as dictionary key works if it is not nested.

struct OuterStruct
{
    enum InnerEnum
    {
        case none
        case a
        case b
    }
}

var dict : [OuterStruct.InnerEnum: String] = [OuterStruct.InnerEnum: String]()

Upvotes: 3

Views: 1223

Answers (1)

Antonio
Antonio

Reputation: 72760

You can fix that by using the traditional way to instantiate a dictionary:

var dict : [OuterStruct.InnerEnum: String] = Dictionary<OuterStruct.InnerEnum, String>()

Note that you can use type inference and avoid specifying the variable type:

var dict = Dictionary<OuterStruct.InnerEnum, String>()

As to why the shorthand syntax doesn't work, I don't have an answer - I think you should file a radar about that. I tried turning InnerEnum into a struct and a class, and the same error is reported.

Upvotes: 4

Related Questions