How to compare NSUUID

What is the best (smallest code, fastest, most reliable) way to compare two NSUUIDs ?

Here is an example:

 -(BOOL)isUUID:(NSUUID*)uuid1 equalToUUID:(NSUUID*)uuid2 { return ... // YES if same or NO if not same } 
+6
source share
4 answers

From the NSUUID class help:

Note. The NSUUID class is not a free bridge using CoreFoundations CFUUIDRef. Use UUID strings to convert between CFUUID and NSUUID, if necessary. Two NSUUID objects cannot be matched by pointer value (like CFUUIDRef); use isEqual: compare two NSUUID instances.

So just use the following:

 -(BOOL)isUUID:(NSUUID*)uuid1 equalToUUID:(NSUUID*)uuid2 { return [uuid1 isEqual:uuid2]; } 
+8
source

You do not need to create an additional method for this, as the documentation states that

NSUUID objects cannot be matched by pointer value (like CFUUIDRef); use isEqual: to compare two instances of NSUUID.

So just BOOL sameUUID = [uuid1 isEqual:uuid2];

+7
source

NSUUID effectively wraps uuid_t.

Decision...

 @implementation NSUUID ( Compare ) - ( NSComparisonResult ) compare : ( NSUUID * ) that { uuid_t x; uuid_t y; [ self getUUIDBytes : x ]; [ that getUUIDBytes : y ]; const int r = memcmp ( x, y, sizeof ( x ) ); if ( r < 0 ) return NSOrderedAscending; if ( r > 0 ) return NSOrderedDescending; return NSOrderedSame; } @end 
+1
source

A reasonable simple way to achieve this is to use string comparisons. However, a method that uses the underlying CFUUIDRef may be faster.

 -(BOOL)isUUID:(NSUUID*)uuid1 equalToUUID:(NSUUID*)uuid2 { return [[uuid1 UUIDString] isEqualToString:[uuid2 UUIDString]]; } 
0
source

All Articles