将数字添加到停靠栏图标

发布于 2024-08-14 16:46:20 字数 521 浏览 5 评论 0原文

这是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴我心暖 2024-08-21 16:46:20

你基于什么理由认为它应该是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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文