为什么在Xcode 10中从枚举获取错误的hashValue?这是苹果的错误吗?

16 浏览
0 Comments

为什么在Xcode 10中从枚举获取错误的hashValue?这是苹果的错误吗?

更新:

我遇到了这个问题,是因为我在Antonio的建议中使用了hashValue来获取枚举计数,在这个问题中。

而且,Antonio的答案在Xcode 10之前有效,我只是想知道为什么结果现在发生了变化。


原始问题

环境:

macOS 10.14.1

相同的枚举,在Xcode 10.1中返回的枚举项的hashValue为4607296766572878277,在Xcode 9.4.1中返回为0:

代码:

enum IntEnum: Int {

case first = 1, second

}

let x = IntEnum.first.hashValue

print("first hashValue \(x)")

enum strEnum: String {

case first, second

}

let a = strEnum.first.hashValue

Xcode 10.1:

enter image description here

Xcode 9.4.1:

enter image description here

这是苹果的bug吗?

0
0 Comments

问题的原因是因为在Xcode 10中,使用hashValue获取枚举值的哈希值时出现错误。这可能是Apple的一个bug。

解决方法是不要在将来的执行过程中使用hashValue。

0