Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KVO and NSMutableArray

How can I set KVO (key-value-observing) with an NSMutableArray?

I want to be notified when a change appears in the array. I never used KVO before with a collection like an array.

like image 629
Zingu Avatar asked Aug 02 '11 15:08

Zingu


People also ask

What is NSMutableArray?

The NSMutableArray class declares the programmatic interface to objects that manage a modifiable array of objects. This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray .

What is difference between NSArray and NSMutableArray?

NSArray creates static arrays, and NSMutableArray creates dynamic arrays.


1 Answers

I think you'll be interested in the answers to this question.

The key is that you can't observe any properties on the array directly—the array is just storage—but you can observe the to-many relationship that's backed by that array (here I'm assuming your array is a property on an object somewhere).

If you don't want to use those special accessors all over the place, your code that owns the array can call

-willChange:valuesAtIndexes:forKey: and

-didChange:valuesAtIndexes:forKey:

as described in this answer.

And if you're on a Mac and not iOS, you should consider NSArrayController.

like image 152
Joe Osborn Avatar answered Oct 15 '22 06:10

Joe Osborn