【Swift 4.2】uuid 取 hashCode(与 Java/Go/Kotlin 一致)
extension String {
func hashCode() -> Int32 {
let components = self.split(separator: "-")
var mostSigBits = Int64(components[0], radix: 16)!
mostSigBits <<= 16
let c1 = Int64(components[1], radix: 16)!
mostSigBits |= c1
mostSigBits <<= 16
let c2 = Int64(components[2], radix: 16)!
mostSigBits |= c2
var leastSigBits = Int64(components[3], radix: 16)!
leastSigBits <<= 48
let c4 = Int64(components[4], radix: 16)!
leastSigBits |= c4
let hilo = mostSigBits ^ leastSigBits
return Int32(truncatingIfNeeded: hilo>>32) ^ Int32(truncatingIfNeeded: hilo)
}
}
在 Playground 里面用 "c2ab81d4-2226-4d0c-a49a-dc59b34f7972" 测试输出 "-145200653"