I'm using an NSArrayController
, NSMutableArray
and NSTableView
to show a list of my own custom objects (although this question probably applies if you're just showing a list of vanilla NSString
objects too).
At various points in time, I need to clear out my array and refresh the data from my data source. However, just calling removeAllObjects
on my NSMutableArray
object does not trigger the KVO updates, so the list on screen remains unchanged.
NSArrayController
has no removeAllObjects
method available, which seems really weird. (It does have addObject
, which I use to add the objects, ensuring the KVO is triggered and the UI is updated.)
The cleanest way I've managed to cause this happen correctly is:
[self willChangeValueForKey:@"myArray"];
[myArray removeAllObjects];
[self didChangeValueForKey:@"myArray"];
...so I'm kind of having to do the KVO notification manually myself (this is in my test app class, that contains the myArray
property, which is NSMutableArray
, as mentioned.)
This seems wrong - is there a better way? From my googling it seems a few people are confused by the lack of removeAllObjects
in NSArrayController
, but haven't seen any better solutions.
I have seen this solution:
[self removeObjectsAtArrangedObjectIndexes:
[NSIndexSet indexSetWithIndexesInRange:
NSMakeRange(0, [[self arrangedObjects] count])]];
but this looks even more unpleasant to me. At least my solution is at least marginally self-documenting.
Did Apple not notice that sometimes people might want to empty a list control being managed via an NSArrayController
object? This seems kind of obvious, so I think I must be missing something...
Aside: of course, if I add new items to the array (via NSArrayController
), then this triggers a KVO update with the NSArrayController/NSTableView
, but:
- Sometimes I don't put any items in the list, because there are none. So you just see the old items.
- This is a bit yucky anyway.