i have an arraycontroller bound to the tableview. i need to return the number of checked checkoxes in the table. the arraycontroller is filled with nsmutabledictionaries. this is the code i have so far:
-(IBAction)getlist:(id)sender{
checkedchecks = 0;
for (NSManagedObject *a in imagescontroller.arrangedObjects)
{
////MISSING CODE GOES HERE
}
NSAlert *alert = [[NSAlert alloc] init] ;
[alert setMessageText:[NSString stringWithFormat:@"%ld",(long)checkedchecks ]];
[alert runModal];
}
now i would need to know how i can count all the values that are boolean and are set to yes.. thanks!
Actually, I think you do not have to loop through your objects.
NSUInteger checked = [(NSNumber*)[imagesController.arrangedObjects
valueForKeyPath:@"@sum.boolProperty"] integerValue];
This is sort of a hack because a BOOL
will be interpreted as 0 or 1.
The semantically more correct way is.
NSUInteger checked = [imagesController.arrangedObjects
filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:
@"boolProperty == %@", @YES]].count;
This is assuming that you have an array (arrangedObjects
) which contains instances of NSManagedObject
(or subclass thereof). The objects have a property called boolProperty
of type NSNumber
(which is the standard wrapper for BOOL values in managed objects). When a row is displayed, it is marked as checked if this boolProperty
is @YES
. If you change the checkmark (e.g. by selecting the row), the model should be updated: the appropriate managed object should be retrieved and the boolProperty
toggled.