Isuru
Isuru

Reputation: 31323

Initialized enum returns wrong hashValue

This is Swift 1.2 and I'm using Xcode 6.4. The following enum has a failable initializer.

enum EstimateItemStatus: Int, Printable {
    case Pending = 1
    case OnHold = 2
    case Done = 3

    var description: String {
        switch self {
        case .Pending: return "Pending"
        case .OnHold: return "On Hold"
        case .Done: return "Done"
        }
    }

    init?(id : Int) {
        switch id {
        case 1:
            self = .Pending
        case 2:
            self = .OnHold
        case 3:
            self = .Done
        default:
            return nil
        }
    }
}

If I pass an ID and initialize an instance, the enum value I get is correct. However the hashValue is wrong. For example,

let status = EstimateItemStatus(id: 2)!
println("\(status.hashValue) - \(status)")

The output I get is 1 - On Hold.

But it should be 2 - On Hold.

What am I doing wrong here? Is this a compiler bug or am I missing something?

Demo playground

Upvotes: 0

Views: 240

Answers (1)

vadian
vadian

Reputation: 285132

Maybe you're mixing up hashValue vs. rawValue.
The hash value is not enforced to be equal to the raw value

Upvotes: 2

Related Questions