【Swift 4.2】uuid 取 hashCode(与 Java/Go/Kotlin 一致) - 农民伯伯
2019-02-25 20:55:00 Author: www.cnblogs.com(查看原文) 阅读量:109 收藏

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"


文章来源: http://www.cnblogs.com/over140/p/10432929.html
如有侵权请联系:admin#unsafe.sh