NSData compared to NSData in percent

I have NSData *object1 and another NSData *object2 . How can I compare these objects with what percentage are they similar? For example: Object1, similar to Object2 in - 99%. Thanks.

+1
source share
3 answers

Receive bytes in both cases and retest how many of them are equal.

 uint8_t* bytes1 = (uint8_t*)[object1 bytes]; uint8_t* bytes2 = (uint8_t*)[object2 bytes]; NSUInteger sameCount = 0; for (NSUInteger i = 0 ; i < MIN([object1 length], [object2 length]) ; ++i) { if (bytes1[i] == bytes2[i]) { sameCount++; } } double fractionSame = (double) sameCount / (double) MIN([object1 length], [object2 length]); 

The above assumes that if one of the data is longer than the other, you do not care about the excess.

+4
source

It really depends on the logic. If you, for example, are trying to compare images (and their data is stored as NSData), then you need to write image comparison algorithms . If this is some other kind of data, you need to define this semantics first. If all else fails, I think @JeremyP's answer should be sufficient.

+1
source

There is no such thing for NSData. You will need to write your own NSSortDescriptor file, optimized for how you want to compare the contents of one NSData with another.

http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSSortDescriptor_Class/Reference/Reference.html

0
source

Source: https://habr.com/ru/post/909164/


All Articles