PDA

View Full Version : Finding the size of an NSArrayController?




Littleodie914
Mar 22, 2007, 05:15 PM
Hey guys, pretty simple question, I've got an NSArrayController, and I'm trying to somehow get the size of it. Right now I'm trying:

[[stepsController arrangedObjects] count]

But it's returning 0. Any ideas? Thanks! :)



HiRez
Mar 23, 2007, 01:47 AM
Hey guys, pretty simple question, I've got an NSArrayController, and I'm trying to somehow get the size of it. Right now I'm trying:

[[stepsController arrangedObjects] count]

But it's returning 0. Any ideas? Thanks! :)Dunno, seems like that should work, and I'm pretty sure I'm using exactly that code in one of my projects. Are you absolutely certain you have more than 0 objects being controlled by the controller? Are you maybe using Core Data (where "count" possibly doesn't reflect objects that haven't been fetched from the data store yet)?

Eraserhead
Mar 23, 2007, 06:14 AM
Dunno, seems like that should work, and I'm pretty sure I'm using exactly that code in one of my projects. Are you absolutely certain you have more than 0 objects being controlled by the controller? Are you maybe using Core Data (where "count" possibly doesn't reflect objects that haven't been fetched from the data store yet)?

Thats what I use too... Try checking the filter predicate is nil, then if it's still nil fetch the objects from the data store (Core Data only), see the following code.

[arrayController setFilterPredicate:nil];
if([[arrayController arrangedObjects] count]==0){
//NSLog(@"no objects");//may be worth uncommenting this.
[arrayController addObjects:[mObjC executeFetchRequest:[model fetchRequestFromTemplateWithName:@"yourObject" substitutionVariables:[NSDictionary dictionary]]
error:nil]];
}


EDIT: mObjC is the managedObjectContext and model is the managedObjectModel