I use NSArrayController , NSMutableArray and NSTableView to show a list of my own custom objects (although this question probably applies if you just show a list of vanilla NSString objects).
At different points in time, I need to clear my array and update data from my data source. However, just calling removeAllObjects on my NSMutableArray does not cause a KVO update, so the list on the screen remains unchanged.
NSArrayController has a non- removeAllObjects method available that seems really weird. (It has addObject , which I use to add objects, ensuring that KVO is launched and the user interface is updated.)
The cleanest way that this succeeded is correct:
[self willChangeValueForKey:@"myArray"]; [myArray removeAllObjects]; [self didChangeValueForKey:@"myArray"];
... so I somehow need to manually enter a KVO notification (this is in my test application class that contains the myArray property, which is NSMutableArray , as mentioned).
This seems wrong - is there a better way? From my googling, it seems that some people get confused due to the lack of removeAllObjects in NSArrayController , but have not seen better solutions.
I saw this solution:
[self removeObjectsAtArrangedObjectIndexes: [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, [[self arrangedObjects] count])]];
but it looks even more unpleasant for me. At least my decision is at least slightly self-documenting.
Has Apple noticed that sometimes people may want to clear the list control through an NSArrayController ? This seems obvious, so I think I'm missing something ...
Also, of course, if I add new elements to the array (via NSArrayController ), this will cause KVO to be updated using NSArrayController/NSTableView , but:
- Sometimes I donβt put any items on the list because they are not. So you just see old objects.
- Anyway, it's a little yucky.
cocoa macos nsarraycontroller
Slacker
source share