如何将UUID存储为int

问题描述 投票:0回答:3

我在这里遇到一些问题..我正在使用以下代码在我的应用程序中生成 uuid。

    - (NSString *)GetUUID
{
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    return [(NSString *)string autorelease];
}

此代码返回一个

NSString
对象。现在我想将生成的
UUID
存储为
unsigned int
。任何建议请我在这里该怎么做。谢谢。

iphone objective-c xcode4 uuid
3个回答
9
投票

根据维基百科页面,UUID 的长度为 128 位。您可以使用的最大 int 值是 64 位。因此,我想说你不能将它存储在 int 中,因为根本没有空间。


7
投票

使用

.UUIDString

将 UUID 转换为字符串

获取字符串的

.hash
属性。

转换为 int。

int numericFormUUID = (int)UUIDString.hash;


注意:与往常一样,哈希可能会发生冲突。两个用户最终可能会得到相同的值,而 UUID 则保证是唯一的。


0
投票

有一个新的

.hashValue
属性。

UUID().hashValue
© www.soinside.com 2019 - 2024. All rights reserved.