• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: [iPhone] How to create a unique string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [iPhone] How to create a unique string


  • Subject: Re: [iPhone] How to create a unique string
  • From: Thomas Wetmore <email@hidden>
  • Date: Thu, 13 May 2010 11:13:12 -0400

UUID is the way to go. Here is a category I use to generate unique ids. It uses core foundation to generate a 128-bit UUID and converts that to a 22-character string. The conventional string form of a UUID is 36 characters long. A goal of this code was to minimize the size of the id since my application generates billions of them.

------------------------

static unichar x (unsigned int);

// This category uses Apple's core foundation to generate a 128-bit universal id and then
// encodes the bits into a 22 character string. The conventional string form of a universal
// id is 36 characters long, so this saves 14 characters.
//----------------------------------------------------------------------------------------
@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 '_';
}

On May 13, 2010, at 11:05 AM, Michael Ash wrote:
>
> CFUUID includes the MAC address, so unless your MAC address is cloned
> or you manage to generate two UUIDs on the same device in the same
> 100ns time interval or the calendar rolls over (which will take about
> 3700 years), they are entirely unique within the universe of CFUUID
> strings.
>
> Mike
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden

Tom Wetmore, Chief Bottle Washer, DeadEnds Software

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >[iPhone] How to create a unique string (From: Tharindu Madushanka <email@hidden>)
 >Re: [iPhone] How to create a unique string (From: John Pannell <email@hidden>)
 >Re: [iPhone] How to create a unique string (From: Jonathan del Strother <email@hidden>)
 >Re: [iPhone] How to create a unique string (From: Tharindu Madushanka <email@hidden>)
 >Re: [iPhone] How to create a unique string (From: Eric Gorr <email@hidden>)
 >Re: [iPhone] How to create a unique string (From: Thomas Davie <email@hidden>)
 >Re: [iPhone] How to create a unique string (From: Michael Ash <email@hidden>)

  • Prev by Date: Re: [iPhone] How to create a unique string
  • Next by Date: Re: How to edit/resize embedded graphics and images in a NSTextView ?
  • Previous by thread: Re: [iPhone] How to create a unique string
  • Next by thread: Re: [iPhone] How to create a unique string
  • Index(es):
    • Date
    • Thread