我找不到任何官方的方式拿回一个UUID字符串CBUUID。 这些uuid可以2到16字节长。 目标是CBUUIDs存储在一个文件作为字符串的某个地方,然后复活(CBUUID UUIDWithString:]等。这是迄今为止我所。
|
这个好像有点问题。。
我设置了以下类别为CBUUID这样做:
@interface CBUUID (StringExtraction)
- (NSString *)representativeString;
@end
@implementation CBUUID (StringExtraction)
- (NSString *)representativeString;
{
NSData *data = [self data];
NSUInteger bytesToConvert = [data length];
const unsigned char *uuidBytes = [data bytes];
NSMutableString *outputString = [NSMutableString stringWithCapacity:16];
for (NSUInteger currentByteIndex = 0; currentByteIndex < bytesToConvert; currentByteIndex++)
{
switch (currentByteIndex)
{
case 3:
case 5:
case 7:
case 9:[outputString appendFormat:@"%02x-", uuidBytes[currentByteIndex]]; break;
default:[outputString appendFormat:@"%02x", uuidBytes[currentByteIndex]];
}
}
return outputString;
}
@end
输入:
NSLog(@"UUID string: %@", [[CBUUID UUIDWithString:@"0bd51666-e7cb-469b-8e4d-2742f1ba77cc"] representativeString]);
NSLog(@"UUID string2: %@", [[CBUUID UUIDWithString:@"1800"] representativeString]);
它产生以下输出:
UUID string: 0bd51666-e7cb-469b-8e4d-2742f1ba77cc
UUID string2: 1800
并保持适当的断字为16字节uuid,同时支持简单的2字节uuid。