Detecting changes in NSArray in ObjC

I need to detect a change in an NSArray object - that is, if any object was added / removed to / from NSArray or was just edited in place. Are there any NSArray built-in hash functions for this task - or do I need to write my own hash function for NSArray? Maybe someone has a different solution? Any ideas?

+3
source share
2 answers

You can use NSArrayControllerwhich Key-Value-Observing is compatible. Unfortunately, NSArrayonly KVC matches. This way you can easily control the property of the array controller arrangedObjects. This should solve your problem.

Also see this question: Key value - observing the attitude towards many in Cocoa

+2
source

All objects have a method -hash, but not all objects have a good implementation.

The NSArray documentation does not determine the result, but testing shows that it returns the length of the array - not very useful:

NSLog(@"%lu", @[@"foo"].hash);              // output: 1
NSLog(@"%lu", @[@"foo", @"bar"].hash);      // output: 2
NSLog(@"%lu", @[@"hello", @"world"].hash);  // output: 2

, <NSCoding>, NSData, -hash:

[NSArchiver archivedDataWithRootObject:@[@"foo"]].hash             // 194519622
[NSArchiver archivedDataWithRootObject:@[@"foo", @"bar"]].hash     // 123459814
[NSArchiver archivedDataWithRootObject:@[@"hello", @"world"]].hash // 215474591

, , -hash. -hash (, , ) , .

+5

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