简体   繁体   中英

Objective C generate random id for object that does not repeat

I want to generate unique ids for object identification, I've come across [[NSUUID UUID] UUIDString] , can it be used for unique identification? or there's a better approach for generating?

Yes: NSUUID generates a unique ID.

From Apple:

UUIDs (Universally Unique Identifiers), also known as GUIDs (Globally Unique Identifiers) or IIDs (Interface Identifiers), are 128-bit values. UUIDs created by NSUUID conform to RFC 4122 version 4 and are created with random bytes.

From RFC 4122 :

A UUID is 128 bits long, and can guarantee uniqueness across space and time.

You may use the following

CFUUIDRef udidCFUUIDRef = CFUUIDCreate(NULL);
NSString *UUIDString = (NSString *) CFBridgingRelease(CFUUIDCreateString(NULL, udidCFUUIDRef));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM