The issue is that, in many cases, you want to programmatically manipulate the contents of the array an NSArrayController is bound to. For things like master-detail interfaces, NSArrayController will do everything for you including manage the array, but for other things — like, say, buddy lists — you'll want to manipulate the array directly and have the changes reflected in your interface.
The problem is that NSMutableArray doesn't actually broadcast Key-Value Observing notifications. I call this a bug, but it's not clear whether or not it'll be fixed. So you can't write code like
[buddies addObject:buddy]; and expect a table managed by an NSArrayController to be updated.Thanks to mmalcolm crawford's notes on the controller layer, I figured out what needs to be done. Essentially, you can either use Key-Value Coding within your class to manipulate your array, or you can use the Key-Value Coding compliant accessor methods to manipulate your array's contents. In other words, you can either do
[[self mutableArrayValueForKey:@"buddies"] addObject:buddy] or you can do[self insertObject:buddy inBuddiesAtIndex:[self countOfBuddies]] assuming you implement -insertObject:inBuddiesAtIndex: and -countOfBuddies.The thing that tripped me up was that KVC compliant accessor methods are not the same as Scripting KVC compliant accessor methods. So don't go looking at the documentation for NSScriptKeyValueCoding as a guide for what to implement to generate KVO notifications. Hopefully another thing that will be considered a bug...
![[info]](http://l-stat.livejournal.com/img/userinfo.gif)
Typo in method names
(Anonymous)
2008-11-28 07:49 am (UTC)