aryaxt
aryaxt

Reputation: 77616

Reference a nested enum in a dictionary?

Doesn't seem like Swift allows nested enums to be referenced from a dictionary. I could simply move the enum out of the class and that would solve the problem, but I need to create another enum names Status under another class. So it needs to be a nested enum

class User {

    enum Status: String {
        case Alive = "alive"
        case Dead = "dead"
    }
}

var userStatuses = [User.Status: String]()

Upvotes: 0

Views: 107

Answers (2)

Jeremy Pope
Jeremy Pope

Reputation: 3352

Try the non-shorthand version.

var statuses = Dictionary<User.Status, String>()

It looks like a bug when using the shorthand version [User.Status: String]() but the non-shorthand version appears to work fine.

Upvotes: 1

Marcus Rossel
Marcus Rossel

Reputation: 3268

It works if you use a typealias:

class User {
    enum Status: String {
        case Alive = "alive"
        case Dead = "dead"
    }
}

typealias Key = User.Status

var myDictionary = [Key: String]()
myDictionary[.Alive] = "something"

println(myDictionary[.Alive]!) // prints "something"

Upvotes: 1

Related Questions