首页 > 编程语言 > 详细

【Swift 4.2】uuid 取 hashCode(与 Java/Go/Kotlin 一致)

时间:2019-02-25 20:23:11      阅读:369      评论:0      收藏:0      [点我收藏+]
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"

【Swift 4.2】uuid 取 hashCode(与 Java/Go/Kotlin 一致)

原文:https://www.cnblogs.com/over140/p/10432929.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!