Search code examples
iphonecocoaiosnsmutablearraykey-value-observing

Observation of order of array elements


There's any chance to observe for order elements of array via KVO?


Solution

  • The NSKeyValueObserving protocol includes KVO notification methods for ordered to-many relationships (i.e. an NSMutableArray property of an object):

    - willChange:valuesAtIndexes:forKey:
    - didChange:valuesAtIndexes:forKey:
    

    So you can see when an array's elements change and then make sure they're in the right order. You might also take a look at key-value validation, which could give you a way to make sure an array stays sorted.