Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 初始化的枚举返回错误的哈希值_Ios_Swift_Enums - Fatal编程技术网

Ios 初始化的枚举返回错误的哈希值

Ios 初始化的枚举返回错误的哈希值,ios,swift,enums,Ios,Swift,Enums,这是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

这是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-保留

但它应该是2-暂停

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


也许你把
hashValue
rawValue
搞混了

哈希值不强制等于原始值

Swift在其
开关
块中是否没有故障?可能您混淆了
哈希值
原始值
。哈希值不强制等于原始值。@瓦迪安:你说得对!如果你能把这个作为答案,我会接受的。谢谢:)@Isuru:Btw,枚举(没有关联的值)自动有一个
init?(rawValue:rawValue)
initializer,您的init方法是不需要的。@MartinR Oh cool。我不知道。谢谢