0

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!

sharkyenergy
  • 3,842
  • 10
  • 46
  • 97

1 Answers1

3

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.

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • thanks! but doesnt seem to work, Always get 0! i also get a warning: Implicit conversion loses integer precision. NSUinteger to int – sharkyenergy Feb 14 '13 at 15:52
  • and on your first one i get the message: Property "intValue" not found on object of type "id" – sharkyenergy Feb 14 '13 at 15:56
  • See my corrections. These were minor issues. If you are getting zero, you did not save the checked bools properly. – Mundi Feb 14 '13 at 16:00
  • Hello! still getting 0. what do you mean by "save the chcked bools property"? i used this code to add the data to the array controller: `[imgdtl setObject:[NSNumber numberWithBool: YES] forKey: [NSString stringWithFormat:@"%@",[a valueForKey:@"agency"]]];` – sharkyenergy Feb 14 '13 at 16:11
  • 1
    sorry but even after googling around i still dont understand your checked bools part.. could you please explain? thanks – sharkyenergy Feb 14 '13 at 16:46
  • is this the part you mean? `[imgdtl setObject:[NSNumber numberWithBool: YES] ...` if so they aready are at yes and its still returning 0 – sharkyenergy Feb 14 '13 at 16:56
  • No. The filtered array should contain objects if the correct property is addressed and there are true bools. According to the above, your entity instance is called `imgdt` (why?) and your `boolProperty` is called `object` (why?). – Mundi Feb 14 '13 at 16:59
  • imgdtl is my image mutable dictionary. i add the values to the dictionary and at the end i use `[imagescontroller addObject:imgdtl];` to add my mutable dictionary to the arraycontroller. at least this is how i was told to do on another question in the past – sharkyenergy Feb 14 '13 at 17:16