priore

Generate a unique id of a NSObject

Oct 8th, 2014
788
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

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×