PDA

View Full Version : clear content from NSArrayController




MrFusion
Dec 8, 2007, 04:47 PM
I want to clear the content from an NSArrayController programmatically, which seems an embarrassingly simple thing to do, yet which eludes me.

The following line crashes my program:
[data removeObjects:[data arrangedObjects]];
and
[data removeObjects:(NSArray *)[data arrangedObjects]];
does the same thing.

With NSMutableArray, you get at least removeAllObjects.



robbieduncan
Dec 8, 2007, 05:45 PM
If data is an NSArrayController then


[data content];


should return the object (an NSArray or NSMutableArray presumably) holding the content so



[[data content] removeAllObjects];



should work.

I believe the reason what your doing doesn't work is that removing an item from the array causes the arrangedObjects array to change and indexing on the two is not the same...

MrFusion
Dec 8, 2007, 05:49 PM
If data is an NSArrayController then


[data content];


should return the object (an NSArray or NSMutableArray presumably) holding the content so



[[data content] removeAllObjects];



should work.

I believe the reason what your doing doesn't work is that removing an item from the array causes the arrangedObjects array to change and indexing on the two is not the same...

I am using the NSArrayController itself to hold the content. My class only has an IBOutlet to a NSArrayController instance.

robbieduncan
Dec 8, 2007, 05:53 PM
Is that working? My understanding is that NSArrayController (and the rest of the NSController classes) implement the controller layer of the MVC paradigm. They do not provide data storage: that is the job of the model layer. I've never seen an NSArrayController without a content array, either set via the content attribute or via bindings (which is the same thing in the end).

MrFusion
Dec 8, 2007, 06:13 PM
Is that working? My understanding is that NSArrayController (and the rest of the NSController classes) implement the controller layer of the MVC paradigm. They do not provide data storage: that is the job of the model layer. I've never seen an NSArrayController without a content array, either set via the content attribute or via bindings (which is the same thing in the end).

It does. :)
But apparently not perfect. I am changing the code to include a model array.

Nutter
Dec 8, 2007, 08:24 PM
Yes, managing your own model array is a good idea.

Make sure that you use -mutableArrayValueForKey: to remove objects from the array.

kainjow
Dec 19, 2007, 11:01 AM
I just thought about this a bit more since I just implemented a quick NSArrayController using a model array (instead of using the array controller directly). If you do it that way, you can just set your model array to nil or an empty array using KVC and it should work fine.