Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare NSUUID

What is the best (least 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
}
like image 676
Joshcodes Avatar asked Dec 11 '13 17:12

Joshcodes


3 Answers

From the NSUUID class reference:

Note: The NSUUID class is not toll-free bridged with CoreFoundation’s CFUUIDRef. Use UUID strings to convert between CFUUID and NSUUID, if needed. Two NSUUID objects are not guaranteed to be comparable by pointer value (as CFUUIDRef is); use isEqual: to compare two NSUUID instances.

So just use the following:

-(BOOL)isUUID:(NSUUID*)uuid1 equalToUUID:(NSUUID*)uuid2
{
    return [uuid1 isEqual:uuid2];
}
like image 86
Fruity Geek Avatar answered Nov 05 '22 11:11

Fruity Geek


You don't need to create an extra method for this, as the documentation states that

NSUUID objects are not guaranteed to be comparable by pointer value (as CFUUIDRef is); use isEqual: to compare two NSUUID instances.

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

like image 23
tilo Avatar answered Nov 05 '22 09:11

tilo


NSUUID effectively wraps uuid_t.

Solution...

@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
like image 3
digitaldaemon Avatar answered Nov 05 '22 11:11

digitaldaemon