【问题标题】:How to generate unique GUID 36 chars in SQLITE iPhone如何在 SQLITE iPhone 中生成唯一的 UUID 36 个字符
【发布时间】:2011-08-30 17:28:11
【问题描述】:

我正在尝试创建一个唯一且具有主键的表。我知道在 sqlite 中我们可以开发唯一的 AUTOINCREMENT ID SQL AUTOINCREMENT,但是是否可以生成 36 个字符长的唯一 GUID。这样做的唯一原因是让它更加独特。

【问题讨论】:

    标签: iphone objective-c sqlite


    【解决方案1】:

    这是我用于 UUID 的代码(我什至可能在 Stack Overflow 上找到它)...

    + (NSString *)GetUUID
    {
        CFUUIDRef theUUID = CFUUIDCreate(NULL);
        CFStringRef string = CFUUIDCreateString(NULL, theUUID);
        CFRelease(theUUID);
        return [(NSString *)string autorelease];
    }
    

    我不知道生成的 UUID 有多长,因为我使用它的方式我不在乎,所以也许可以通过将结果传递给 NSLog 调用来检查。

    HTH,佩德罗 :)

    【讨论】:

      【解决方案2】:

      我使用此代码在 iphone 上生成 guid - NSString 上的类别。不记得我在哪里找到的,但效果很好。

      #import "NSString_UniqueID.h"
      
      
      static unichar x (unsigned int); 
      
      @implementation NSString (TWUUID) 
      
      + (NSString*) stringWithUniqueId 
      { 
          CFUUIDRef uuid = CFUUIDCreate(NULL); 
          CFUUIDBytes b = CFUUIDGetUUIDBytes(uuid); 
          unichar unichars[22]; 
          unichar* c = unichars; 
          *c++ = x(b.byte0 >> 2); 
          *c++ = x((b.byte0 & 3 << 4) + (b.byte1 >> 4)); 
          *c++ = x((b.byte1 & 15 << 2) + (b.byte2 >> 6)); 
          *c++ = x(b.byte2 & 63); 
          *c++ = x(b.byte3 >> 2); 
          *c++ = x((b.byte3 & 3 << 4) + (b.byte4 >> 4)); 
          *c++ = x((b.byte4 & 15 << 2) + (b.byte5 >> 6)); 
          *c++ = x(b.byte5 & 63); 
          *c++ = x(b.byte6 >> 2); 
          *c++ = x((b.byte6 & 3 << 4) + (b.byte7 >> 4)); 
          *c++ = x((b.byte7 & 15 << 2) + (b.byte8 >> 6)); 
          *c++ = x(b.byte8 & 63); 
          *c++ = x(b.byte9 >> 2); 
          *c++ = x((b.byte9 & 3 << 4) + (b.byte10 >> 4)); 
          *c++ = x((b.byte10 & 15 << 2) + (b.byte11 >> 6)); 
          *c++ = x(b.byte11 & 63); 
          *c++ = x(b.byte12 >> 2); 
          *c++ = x((b.byte12 & 3 << 4) + (b.byte13 >> 4)); 
          *c++ = x((b.byte13 & 15 << 2) + (b.byte14 >> 6)); 
          *c++ = x(b.byte14 & 63); 
          *c++ = x(b.byte15 >> 2); 
          *c = x(b.byte15 & 3); 
          CFRelease(uuid); 
          return [NSString stringWithCharacters: unichars length: 22]; 
      } 
      @end 
      // Convert six-bit values into letters, numbers or _ or $ (64 characters in that set). 
      //------------------------------------------------------------------------------------ 
      unichar x (unsigned int c) 
      { 
          if (c < 26) return 'a' + c; 
          if (c < 52) return 'A' + c - 26; 
          if (c < 62) return '0' + c - 52; 
          if (c == 62) return '$'; 
          return '_'; 
      } 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-03
        • 2018-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多