Adding a number to the dock icon
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 a开发者_JAVA技巧dding 8 to the dock icon when it should be 2
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.
精彩评论