gpt4 book ai didi

ios - 初始化的枚举返回错误的 hashValue

转载 作者:行者123 更新时间:2023-11-28 13:03:16 26 4
gpt4 key购买 nike

这是 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。

我在这里做错了什么?这是编译器错误还是我遗漏了什么?

Demo playground

最佳答案

也许您混淆了 hashValuerawValue
哈希值不强制等于原始值

关于ios - 初始化的枚举返回错误的 hashValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33387336/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com