A GUID (globally unique identifier) is a 128-bit text string that represents an identification (ID). Organizations generate GUIDs when a unique reference number is needed to identify information on a computer or network. A GUID can be used to ID hardware, software, accounts, documents and other items.
Thus, the probability to find a duplicate within 103 trillion version-4 UUIDs is one in a billion.
No, a UUID can't be guaranteed to be unique. A UUID is just a 128-bit random number. When my computer generates a UUID, there's no practical way it can prevent your computer or any other device in the universe from generating that same UUID at some time in the future.
UUIDs are handled in Core Foundation, by the CFUUID library. The function you are looking for is CFUUIDCreate.
FYI for further searches: these are most commonly known as UUIDs, the term GUID isn't used very often outside of the Microsoft world. You might have more luck with that search term.
At least on MacOSX 10.5.x you might use the command line tool "uuidgen" to get your string e.g.
$ uuidgen
054209C4-3873-4679-8104-3C18AE780512
there's also an option -hdr with this comand that conveniently generates it in header style
See man-page for further infos.
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.
or there's the uuidgen
command line tool.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With