ios 如何在ios中生成UUID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14352418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:52:14  来源:igfitidea点击:

How to generate UUID in ios

iosobjective-c

提问by Saurabh Jadhav

How to get a UUID in objective c, like in Java UUID is used to generate unique random numbers which represents 128 bit value.

如何在目标 c 中获取 UUID,就像在 Java 中一样 UUID 用于生成代表 128 位值的唯一随机数。

回答by Raptor

Try:

尝试:

CFUUIDRef udid = CFUUIDCreate(NULL);
NSString *udidString = (NSString *) CFUUIDCreateString(NULL, udid);

UPDATE:

更新:

As of iOS 6, there is an easier way to generate UUID. And as usual, there are multiple ways to do it:

从 iOS 6 开始,有一种更简单的方法来生成 UUID。和往常一样,有多种方法可以做到:

Create a UUID string:

创建一个 UUID 字符串:

NSString *uuid = [[NSUUID UUID] UUIDString];

Create a UUID:

创建一个 UUID:

[NSUUID UUID]; // which is the same as..
[[NSUUID] alloc] init]; 

Creates an object of type NSConcreteUUIDand can be easily casted to NSString, and looks like this: BE5BA3D0-971C-4418-9ECF-E2D1ABCB66BE

创建一个类型的对象,NSConcreteUUID可以很容易地转换为NSString,如下所示:BE5BA3D0-971C-4418-9ECF-E2D1ABCB66BE

NOTE from the Documentation:

文档中的注意事项:

Note:The NSUUID class is not toll-free bridged with CoreFoundation's CFUUIDRef. Use UUID strings to convert between CFUUID and NSUUID, if needed. Two NSUUID objects are not guaranteed to be comparable by pointer value (as CFUUIDRef is); use isEqual: to compare two NSUUID instances.

注意:NSUUID 类不是与 CoreFoundation 的 CFUUIDRef 桥接的免费电话。如果需要,使用 UUID 字符串在 CFUUID 和 NSUUID 之间进行转换。不能保证两个 NSUUID 对象可以通过指针值进行比较(就像 CFUUIDRef 一样);使用 isEqual:比较两个 NSUUID 实例。

回答by Melvin

Swift version of Raptor's answer:

Raptor 答案的Swift 版本:

let uuid = NSUUID().UUIDString

回答by Bergasms

+ (NSString *)uniqueFileName
{
    CFUUIDRef theUniqueString = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUniqueString);
    CFRelease(theUniqueString);
    return [(NSString *)string autorelease];
}

回答by Dominic Sander

-(NSString*) myUUID()
{
    CFUUIDRef newUniqueID = CFUUIDCreate(kCFAllocatorDefault);
    CFStringRef newUniqueIDString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueID);
    NSString *guid = (__bridge NSString *)newUniqueIDString;
    CFRelease(newUniqueIDString);
    CFRelease(newUniqueID);
    return([guid lowercaseString]);
}

回答by Fabio Caccamo

I suggest you to check this library: https://github.com/fabiocaccamo/FCUUID

我建议你检查这个库:https: //github.com/fabiocaccamo/FCUUID

It provides very simple API to get universally unique identifiers with different persistency levels.

它提供了非常简单的 API 来获取具有不同持久性级别的通用唯一标识符。

It is compatible with: iOS5, iOS6, iOS7, iOS8

兼容:iOS5、iOS6、iOS7、iOS8

回答by Veeru Sharma

you can use CFUUID for iOS 5 or lower version and NSUUID for iOS 6 and 7. for making it more secure you can store your UUID in keychain

您可以在 iOS 5 或更低版本中使用 CFUUID,在 iOS 6 和 7 中使用 NSUUID。为了使其更安全,您可以将 UUID 存储在钥匙串中

回答by Sargis Gevorgyan

- (NSString*)generateGUID{
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    return [NSString stringWithFormat:@"%@", string];
}

回答by Sureshkumar Linganathan

For Swift 5.0, Use this,

对于 Swift 5.0,使用这个,

    let uuidRef = CFUUIDCreate(nil)
    let uuidStringRef = CFUUIDCreateString(nil, uuidRef)
    let uuid = uuidStringRef as String? ?? ""