这是 Swift 1.2,我使用的是 Xcode 6.4。以下枚举具有可失败的初始化程序。
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
}
}
}
如果我传递一个 ID 并初始化一个实例,我得到的枚举值是正确的。但是 hashValue
是错误的。例如,
let status = EstimateItemStatus(id: 2)!
println("\(status.hashValue) - \(status)")
我得到的输出是1 - On Hold。
但它应该是 2 - On Hold。
我在这里做错了什么?这是编译器错误还是我遗漏了什么?
最佳答案
也许您混淆了 hashValue
和 rawValue
。
哈希值不强制等于原始值
关于ios - 初始化的枚举返回错误的 hashValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33387336/