NSArrayController changes: notification not sent.


Is there a way to add an observer to be notified if the content array of a NSArrayController is changed? I tried (in NSDocument):

    [self addObserver:myArrayController forKeyPath:@"count" options:0 context:nil];

but the notification is not sent when I add/remove records to/from the controller.


I guess you mean the key path arrangedObjects.@count,
you can bind properties (for example the enabled property of a minus button) directly in Interface builder

Hi Stefan!

I have only bound the minus button to the remove: method of the controller, my NSDocument has it own addRecord: method because I want to put some default values into the record :slight_smile:

Now, I thought this notification was useful to set the document “dirty” flag (updateChangeCount). But I think I’m in this “Doing It Wrongâ„¢” manner described on this site.http://lists.apple.com/archives/cocoa-dev/2011/Jan/msg00188.html. :frowning:

Well, after reading, I think the cleanest way is to define my own removeRecord: method into NSDocument and set the updateChangeCount in both add/remove methods. What do you think? And if, in a distant future, I want to implement Undo, it’s the best place to put it also.

Well, I forgot there is no such restriction to observers: objects can observe themselves! So I put this:

When the controller’s array is changed (records added or deleted), the document is marked as changed.

All the best,