views:

94

answers:

1

Here is my code:

for (NSManagedObject *object in array) {
 if ([[object valueForKey:@"DueDate"] isLessThan:[NSDate date]]) {
  count++;
  NSLog(@"Looped");
  NSString *test = [[NSString alloc] initWithFormat:@"%@", [object valueForKey:@"DueDate"]];
  NSLog(@"%@", test);
 }
}
NSLog(@"%i", count);
NSDockTile *aTitle = [[NSApplication sharedApplication] dockTile];
[aTitle setBadgeLabel:[NSString stringWithFormat:@"%i", count]];

For some reason this code is adding 8 to the dock icon when it should be 2

+3  A: 

On what grounds do you claim that it should be 2? You clearly have eight objects in the array whose due date is less than the current date (which you create a new object for each time through the loop, BTW).

What's the class of the values of these managed objects' DueDate property? (Don't look at your model for this—send the due date values class messages and log the results using NSLog.) It's possible that they're not NSDates, and that their compare: method is, instead of throwing an exception when asked to compare to an NSDate, simply returning nonsense.

Furthermore, why not include this is-less-than-X-date test as the predicate in the fetch request you're using to get these objects? Then (after making sure the due date values are NSDates) you could simply use the count of the array. That's assuming you aren't doing something else with the larger result array outside of the code you showed, of course.

Peter Hosey
The DueDate class NSLog returns __NSCFDate. so I take that is a NSDate?
nanochrome
Probably. The way to test that would be to log the class of `[NSDate date]` as well.
Peter Hosey
It's a __NSCFDate as well
nanochrome
Then yes, they are both NSDates. Unless you can find another problem with it, I can only conclude that you have more dates that are earlier than the current date than you think you have. You can verify this by logging the dates themselves (`[array valueForKey:@"DueDate"]`).
Peter Hosey
It returns 8.......... I just don't have a clue what is wrong
nanochrome
No, `[array valueForKey:@"DueDate"]` does not return 8. It returns an array, unless `array` is not actually an array (in which case you either have named that variable poorly or have a memory-management problem). If you meant `count` being 8, nothing is wrong that I can see; you simply have eight dates that are less than the current date. Logging the items' `DueDate` (using the `[array valueForKey:@"DueDate"]` message) will prove that.
Peter Hosey