将数字添加到停靠栏图标
这是我的代码:
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]];
出于某种原因,此代码将 8 添加到停靠栏图标,而它应该是 2
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你基于什么理由认为它应该是2?显然,数组中有八个对象,其到期日期小于当前日期(顺便说一句,您每次通过循环都创建一个新对象)。
这些托管对象的
DueDate
属性的值属于什么类别? (不要为此查看您的模型 - 发送到期日期值class
消息并使用NSLog
记录结果。)它们可能不是 NSDates,并且他们的compare:
方法在被要求与 NSDate 进行比较时不会抛出异常,而是简单地返回无意义的内容。此外,为什么不将此 is-than-X-date 测试作为谓词包含在您用于获取这些对象的获取请求中?然后(在确保截止日期值是 NSDates 后)您可以简单地使用数组的
count
。当然,这是假设您没有在显示的代码之外对较大的结果数组执行其他操作。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 valuesclass
messages and log the results usingNSLog
.) It's possible that they're not NSDates, and that theircompare:
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.