priore

Generate a unique id of a NSObject

Oct 8th, 2014
703
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import <CommonCrypto/CommonDigest.h>
  2.  
  3. @implementation NSObject (Utility)
  4.  
  5. - (NSString*)uniqueID
  6. {
  7.     // nsobject --> nsdata -- > md5 hash --> hex string (30 chars)
  8.     NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self];
  9.     unsigned char result[CC_MD5_DIGEST_LENGTH];
  10.     CC_MD5([data bytes], (uint32_t)[data length], result);
  11.     return [NSString stringWithFormat:
  12.             @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  13.             result[0], result[1], result[2], result[3],
  14.             result[4], result[5], result[6], result[7],
  15.             result[8], result[9], result[10], result[11],
  16.             result[12], result[13], result[14], result[15]
  17.             ];
  18. }
  19.  
  20. @end
RAW Paste Data