Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MrFusion

macrumors 6502a
Original poster
Jun 8, 2005
613
0
West-Europe
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:
Code:
[data removeObjects:[data arrangedObjects]];
and
Code:
[data removeObjects:(NSArray *)[data arrangedObjects]];
does the same thing.

With NSMutableArray, you get at least removeAllObjects.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
If data is an NSArrayController then

Code:
[data content];

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

Code:
[[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

macrumors 6502a
Original poster
Jun 8, 2005
613
0
West-Europe
If data is an NSArrayController then

Code:
[data content];

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

Code:
[[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

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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

macrumors 6502a
Original poster
Jun 8, 2005
613
0
West-Europe
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

macrumors 6502
Mar 31, 2005
432
0
London, England
Yes, managing your own model array is a good idea.

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

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.