您的位置:首页 > 产品设计 > UI/UE

iOS 生成 UUID(GUID)

2013-03-27 11:54 471 查看
iOS 生成 UUID(或者叫GUID)例子代码

NSString * gen_uuid()

{

CFUUIDRef uuid_ref = CFUUIDCreate(NULL);

CFStringRef uuid_string_ref= CFUUIDCreateString(NULL, uuid_ref);

CFRelease(uuid_ref);

NSString *uuid = [NSString stringWithString:(NSString*)uuid_string_ref];

CFRelease(uuid_string_ref);

return uuid;

}

在oc中,uuid的表示

1,用一个16个字节的数组表示

//guid

typedef struct {

#define KAUTH_GUID_SIZE 16/* 128-bit identifier */

unsignedchar g_guid[KAUTH_GUID_SIZE];
} guid_t;

//uuid
typedef
unsigned char
__darwin_uuid_t[16];//定义了一个元素类型为unsigned char,含有16个元素的数组类型__darwin_uuid_t,以后使用__darwin_uuid_t定义的类型都是一个含有16个字符的数组

typedef __darwin_uuid_t
uuid_t;

2,使用结构体表示

typedef struct {
UInt8 byte0;
UInt8 byte1;
UInt8 byte2;
UInt8 byte3;
UInt8 byte4;
UInt8 byte5;
UInt8 byte6;
UInt8 byte7;
UInt8 byte8;
UInt8 byte9;
UInt8 byte10;
UInt8 byte11;
UInt8 byte12;
UInt8 byte13;
UInt8 byte14;
UInt8 byte15;
} CFUUIDBytes;

3,在iOS6.0之后,可以使用系统自带的NSUUID类
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: