ios NSUUID 到 NSString

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21722841/
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 23:25:13  来源:igfitidea点击:

NSUUID to NSString

iosobjective-cuuid

提问by Biribu

I need to get deviceUUID from a phone in a NSString format. Now I have this:

我需要从手机以 NSString 格式获取 deviceUUID。现在我有这个:

NSString *deviceId = [UIDevice currentDevice].identifierForVendor;

Because what I had before, which was:

因为我之前所拥有的是:

NSString *deviceId = [UIDevice currentDevice].uniqueIdentifier;

Gives me an error now.

现在给我一个错误。

But with the first sentence, I got an alert:

但是在第一句话中,我收到了警报:

Incompatible pointer types initializing 'NSString *' with an expression of type 'NSUUID *'

回答by Matthias Bauch

As the error tells you, identifierForVendorreturns an object of class NSUUID, not a NSString.

正如错误告诉你的,identifierForVendor返回一个 class 的对象NSUUID,而不是一个 NSString。

If you need a NSString use this:

如果你需要一个 NSString 使用这个:

NSUUID *identifierForVendor = [[UIDevice currentDevice] identifierForVendor];
NSString *deviceId = [identifierForVendor UUIDString];

回答by onmyway133

For anyone looking for how to convert NSUUID to NSString

对于任何正在寻找如何将 NSUUID 转换为 NSString 的人

ObjC

对象

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

Swift

迅速

NSUUID().UUIDString

Swift 3

斯威夫特 3

UUID().uuidString

回答by iPatel

EDITE :As OP'srequirement I reopen my deleted answer and I mention here @Matthias'sanswer is correct and by using following code you get new device_id each time whenever run this code.

编辑:作为OP 的要求,我重新打开我删除的答案,我在这里提到@Matthias答案是正确的,通过使用以下代码,每次运行此代码时,您都会获得新的 device_id。

Use following code:

使用以下代码:

CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
NSString *uuidString = (NSString *)CFBridgingRelease(CFUUIDCreateString(NULL,uuidRef));
CFRelease(uuidRef);
NSLog(@"%@", uuidString);