How to create a GUID?

  • Dear everyone,
      I feel kind of embarrassed to ask, but could someone lead me the
    way on how to create an globally unique ID (such as for example
    d73e066c-cc88-4d66-be42-94dc091bb571) ?

    Best regards and thanks to everyone.
    Meik
  • CFUUIDCreate() followed by CFUUIDCreateString()

    Mike.

    On 6 Sep 2008, at 22:39, Meik Schuetz wrote:

    > Dear everyone,
    > I feel kind of embarrassed to ask, but could someone lead me the
    > way on how to create an globally unique ID (such as for example
    > d73e066c-cc88-4d66-be42-94dc091bb571) ?
    >
    > Best regards and thanks to everyone.
    > Meik
  • On Sep 6, 2008, at 5:39 PM, Meik Schuetz wrote:

    > I feel kind of embarrassed to ask, but could someone lead me the
    > way on how to create an globally unique ID (such as for example
    > d73e066c-cc88-4d66-be42-94dc091bb571) ?

      In addition to Mike's response, remember that Google is your
    friend ... and so is cocoadev.com. :-)

    http://www.cocoadev.com/index.pl?IDentifiers

      At the bottom of the page, there's this:

    - (NSString *)uuid
    {
    CFUUIDRef uuidRef = CFUUIDCreate(NULL);
    CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
    CFRelease(uuidRef);
    NSString *uuid = [NSString stringWithString:(NSString *)
    uuidStringRef];
    CFRelease(uuidStringRef);
    return uuid;
    }

    --
    I.S.
  • On Sat, Sep 6, 2008 at 10:55 PM, I. Savant <idiotsavant2005...> wrote:
    > At the bottom of the page, there's this:
    >
    > - (NSString *)uuid
    > {
    > CFUUIDRef uuidRef = CFUUIDCreate(NULL);
    > CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
    > CFRelease(uuidRef);
    > NSString *uuid = [NSString stringWithString:(NSString *)
    > uuidStringRef];
    > CFRelease(uuidStringRef);
    > return uuid;
    > }

    Why the unnecessary copy?

    - (NSString *)uuid
    {
          CFUUIDRef uuidRef = CFUUIDCreate(NULL);
          CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
          CFRelease(uuidRef);
          return [(NSString *)uuidStringRef autorelease];
    }

    -- Finlay
  • On Sep 6, 2008, at 7:06 PM, Finlay Dobbie wrote:

    > Why the unnecessary copy?
    >
    > - (NSString *)uuid
    > {
    > CFUUIDRef uuidRef = CFUUIDCreate(NULL);
    > CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
    > CFRelease(uuidRef);
    > return [(NSString *)uuidStringRef autorelease];
    > }

      I didn't write or verify the code, but you appear to be absolutely
    right. Maybe you'd like to update the wiki?

    --
    I.S.
  • On Sat, Sep 6, 2008 at 5:06 PM, I. Savant <idiotsavant2005...> wrote:
    > On Sep 6, 2008, at 7:06 PM, Finlay Dobbie wrote:
    >
    >> Why the unnecessary copy?
    >>
    >> - (NSString *)uuid
    >> {
    >> CFUUIDRef uuidRef = CFUUIDCreate(NULL);
    >> CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
    >> CFRelease(uuidRef);
    >> return [(NSString *)uuidStringRef autorelease];
    >> }
    >
    >
    > I didn't write or verify the code, but you appear to be absolutely right.
    > Maybe you'd like to update the wiki?

    The original works with both GC and non-GC, while the version with
    autorelease will leak under GC (because the string is never
    CFRelease'd). In order to work with both runtimes, either use the
    original, or do this:

    - (NSString *)uuid
    {
        CFUUIDRef uuidRef = CFUUIDCreate(NULL);
        CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
        CFRelease(uuidRef);
        return [NSMakeCollectable(uuidStringRef) autorelease];
    }

    --
    Clark S. Cox III
    <clarkcox3...>
  • > - (NSString *)uuid
    > {
    > CFUUIDRef uuidRef = CFUUIDCreate(NULL);
    > CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
    > CFRelease(uuidRef);
    > return [NSMakeCollectable(uuidStringRef) autorelease];
    > }

    the receiver of this -uuid message is a NSString _instance_ – is
    there any reason why this could not be implemented as a class method

    + (NSString *)uuid { /* same as above */ }

    or as a free function

    NSString * MYuuid() { /* same as above */ }

    ?

    Klaus
  • On Sep 7, 2008, at 4:42 AM, Klaus Backert wrote:

    > the receiver of this -uuid message is a NSString _instance_ – is
    > there any reason why this could not be implemented as a class method

      Somewhere on cocoadev, I remember there is an NSString category
    with a uuid class method. Probably easy to find with a search.

    --
    I.S.
  • > is
    > there any reason why this could not be implemented as a class method

    It really *should* be a class method.

    --
    Scott Ribe
    <scott_ribe...>
    http://www.killerbytes.com/
    (303) 722-0567 voice
  • @implementation NSString (KKUUIDString)

    + (NSString *) UUIDString
    {
    CFUUIDRef UIDRef;
    NSString *uidString;
    UIDRef = CFUUIDCreate(NULL);
    uidString = (NSString *)CFUUIDCreateString(NULL, UIDRef);
    CFRelease(UIDRef);
    return [uidString autorelease];
    }
    @end

    On Sep 7, 2008, at 5:43 PM, <cocoa-dev-request...> wrote:

    > Message: 12
    > Date: Sun, 07 Sep 2008 12:57:26 -0500
    > From: Scott Ribe <scott_ribe...>
    > Subject: Re: How to create a GUID?
    > To: Klaus Backert <Klaus.Backert...>,
    > <Cocoa-dev...>
    > Message-ID: <C4E98036.A2467%<scott_ribe...>
    > Content-Type: text/plain;    charset="US-ASCII"
    >
    >> is
    >> there any reason why this could not be implemented as a class method
    >
    > It really *should* be a class method.
    >
    > --
    > Scott Ribe
    > <scott_ribe...>
    > http://www.killerbytes.com/
    > (303) 722-0567 voice
    >
  • On Sep 6, 2008, at 2:39 PM, Meik Schuetz wrote:

    > Dear everyone,
    > I feel kind of embarrassed to ask, but could someone lead me the
    > way on how to create an globally unique ID (such as for example
    > d73e066c-cc88-4d66-be42-94dc091bb571) ?

    In addition to all the CFUUID solutions, there's also [[NSProcessInfo
    processInfo] globallyUniqueString]; a one-liner, but not thread safe.

    --
    Adam
  • > On Sat, 6 Sep 2008, Meik Schuetz said:
    > Dear everyone,
    > I feel kind of embarrassed to ask, but could someone lead me the
    > way on how to create an globally unique ID (such as for example
    > d73e066c-cc88-4d66-be42-94dc091bb571) ?
    >
    > Best regards and thanks to everyone.
    > Meik

    Since you don't say whether you want to do at run-time or in
    programmer time, and others have explained how to do it
    programmatically, I'll just throw in the obvious: From Terminal, you
    can type "uuidgen". If you do this often, you can do this:

    • Select Edit User Scripts from the User Scripts menu (identified by
    the script icon in the Xcode menu bar).
    • Select Code from the list of script types.
    • Click the Add button and select Shell Script.
    • Double click the name Shell Script and revise it to GenUUID or some
    other appropriate name.
    • Add the line echo -n `uuidgen` to the script .
    • Change the Output option to Replace Selection.
    • Close the Edit User Scripts window.

    You can now generate a UUID in Xcode by choosing your new script from
    the Code submenu of the User Scripts menu.
    You can also assign a keystroke to make this even easier.

    Cheers!
    Dave Howell, President
    Avatron Software, Inc.
    (Check out our new Air Sharing app for the iPhone, at http://www.avatron.com/products/airsharing)
  • On Sep 7, 2008, at 10:44 PM, Adam R. Maxwell wrote:

    > In addition to all the CFUUID solutions, there's also
    > [[NSProcessInfo processInfo] globallyUniqueString]; a one-liner, but
    > not thread safe.

      This, according to the documentation, "includes the host name,
    process ID, and a time stamp, which ensures that the ID is unique for
    the network."

      I admit that I do not know "everything there is to know" about the
    subject, but as I recall from the very wiki page I originally posted,
    this is not "unique enough" across all computers in the world.

      Can anybody verify this?

    --
    I.S.
  • On Mon, Sep 8, 2008 at 8:28 PM, I. Savant <idiotsavant2005...> wrote:

    > On Sep 7, 2008, at 10:44 PM, Adam R. Maxwell wrote:
    >
    > In addition to all the CFUUID solutions, there's also [[NSProcessInfo
    >> processInfo] globallyUniqueString]; a one-liner, but not thread safe.
    >>
    >
    > This, according to the documentation, "includes the host name, process ID,
    > and a time stamp, which ensures that the ID is unique for the network."

    >
    > I admit that I do not know "everything there is to know" about the
    > subject, but as I recall from the very wiki page I originally posted, this
    > is not "unique enough" across all computers in the world.
    >
    > Can anybody verify this?

    On Leopard (and possibly earlier–I haven't checked), it appears that the
    string returned is actually a UUID created with the CFUUID functions
    concatenated with the process ID and the result of mach_absolute_time() so
    the result isn't a proper UUID. I'm not sure where the host name is coming
    from; it's possible that the CFUUID functions incorporate it somehow.

    -- Chris
  • On 9/6/08 7:51 PM, Clark Cox said:

    > The original works with both GC and non-GC, while the version with
    > autorelease will leak under GC (because the string is never
    > CFRelease'd). In order to work with both runtimes, either use the
    > original, or do this:
    >
    > - (NSString *)uuid
    > {
    > CFUUIDRef uuidRef = CFUUIDCreate(NULL);
    > CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
    > CFRelease(uuidRef);
    > return [NSMakeCollectable(uuidStringRef) autorelease];
    > }

    One last correction: CFUUIDCreate() may return null, and one can't pass
    null to CFRelease().  So an "if (uuidRef)" is needed.  (You can pass
    null to NSMakeCollectable, but not to CFMakeCollectable).

    --
    ____________________________________________________________
    Sean McBride, B. Eng                <sean...>
    Rogue Research                        www.rogue-research.com
    Mac Software Developer              Montréal, Québec, Canada
  • On Tue, Sep 9, 2008 at 12:13 AM, Chris Suter <csuter...> wrote:
    > On Leopard (and possibly earlier–I haven't checked), it appears that the
    > string returned is actually a UUID created with the CFUUID functions
    > concatenated with the process ID and the result of mach_absolute_time() so
    > the result isn't a proper UUID. I'm not sure where the host name is coming
    > from; it's possible that the CFUUID functions incorporate it somehow.
    >

    The documentation doesn't specify any particular format for the string
    returned, only the information used to seed it---I wouldn't recommend
    depending on this method always returning a UUID incase this changes
    (even though it probably won't).

    Phil
previous month september 2008 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30          
Go to today