本文给ios开发者讲解一个小技巧,如何使用Objective-C创建UUID的代码。

  UUID含义是通用唯一识别码 (Universally Unique Identifier),这 是一个软件建构的标准,也是被开源软件基金会 (Open Software Foundation, OSF) 的组织在分布式计算环境 (Distributed Computing Environment, DCE) 领域的一部份。

  UUID 的目的,是让分布式系统中的所有元素,都能有唯一的辨识资讯,而不需要透过中央控制端来做辨识资讯的指定。如此一来,每个人都可以建立不与其它人冲突的 UUID。在这样的情况下,就不需考虑数据库建立时的名称重复问题。目前最广泛应用的 UUID,即是微软的 Microsoft's Globally Unique Identifiers (GUIDs),而其他重要的应用,则有 Linux ext2/ext3 档案系统、LUKS 加密分割区、GNOME、KDE、Mac OS X 等等。

- (NSString * )createUUID
{
  
// Create universally unique identifier ( object )
  CFUUIDRef uuidObject
= CFUUIDCreate(kCFAllocatorDefault);

  
// Get the string representation of CFUUID object .
  NSString
* uuidStr = [(NSString * )CFUUIDCreateString(kCFAllocatorDefault, uuidObject) autorelease];

  
// If needed, here is how to get a representation in bytes, returned as a structure
  
// typedef struct {
  
//    UInt8 byte0;
  
//    UInt8 byte1;
  
//    ...
  
//    UInt8 byte15;
  
// } CFUUIDBytes;
  CFUUIDBytes bytes
= CFUUIDGetUUIDBytes(uuidObject);

  CFRelease(uuidObject);

  return uuidStr;
}