Is there a method to generate a standard 128bit GUID (UUID) on the Mac?

后端 未结 7 957
花落未央
花落未央 2020-12-23 21:31

Is there a built in function equivalent to .NET\'s

Guid.NewGuid();

in Cocoa?

My desire is to produce a string along the lines of

7条回答
  •  爱一瞬间的悲伤
    2020-12-23 22:13

    Some code:

    For a string UUID, the following class method should do the trick:

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

    if you really want the bytes (not the string):

    +(CFUUIDBytes)UUIDBytes {
        CFUUIDRef theUUID = CFUUIDCreate(NULL);
        CFUUIDBytes bytes = CFUUIDGetUUIDBytes(theUUID);
        CFRelease(theUUID);
        return bytes;
    }
    

    where CFUUIDBytes is a struct of the UUID bytes.

提交回复
热议问题