11

我需要NSData从不vendorIdentifier转换为NSString清除字节的情况下获取。如何NSUUID使用getUUIDBytes:to转换NSData

4

3 回答 3

21
NSUUID *vendorIdentifier = [[UIDevice currentDevice] identifierForVendor];
uuid_t uuid;
[vendorIdentifier getUUIDBytes:uuid];

NSData *vendorData = [NSData dataWithBytes:uuid length:16];
于 2013-10-03T23:12:40.333 回答
8

使用它来获取vendorIdentifieras Data(Swift 4):

var uuidBytes = UIDevice.current.identifierForVendor!.uuid
let uuidData = NSData(bytes: &uuidBytes, length: 16) as Data

请记住,如文档UIDevice.current.identifierForVendor所述,请注意不要为零。


原始答案(Swift 2 版本):

var uuidBytes: [UInt8] = [UInt8](count: 16, repeatedValue: 0)
UIDevice.currentDevice().identifierForVendor!.getUUIDBytes(&uuidBytes)
let uuidData = NSData(bytes: &uuidBytes, length: 16)
于 2015-10-16T21:07:16.847 回答
1

斯威夫特 3 版本:

let vendorId = UIDevice.current.identifierForVendor!
let uuidBytes = Mirror(reflecting: vendorId.uuid).children.map { $0.1 as! UInt8 }
let data = Data(bytes: uuidBytes)
于 2017-02-28T23:03:13.073 回答