核心数据 - 带日期的谓词
我很难尝试为“最近完成”的任务编写谓词,即显示该任务是否在过去 7 天内完成。我想我需要做这样的事情:“if NOW < dateCompleted + 7 days”。
dateCompleted 是表上的一个属性,但我不确定如何获取它的值并从谓词中向其添加 7 天。我想我需要在编写 NSPredicate 之前先获取属性值,但是如何获取呢?我目前无权访问 ManagedObject。
这可能是接近的解决方案,但我不知道如何定义“oneWeek”,并且我不认为您可以在定义谓词时添加值:
NSPredicate *pred = [NSPredicate predicateWithFormat:@"%@ < todoCompletedDate + %@", [NSDate date], oneWeek];
I'm stumped trying to write a predicate for "Recently Completed" tasks, i.e. show the task if it was completed within the last 7 days. I think I need to do something like this: "if NOW < dateCompleted + 7 days".
The dateCompleted is an attribute on the table, but I'm not sure how I'm supposed to get it's value and add 7 days to it from within the predicate. I guess I need to fetch the attribute value first before writing the NSPredicate, but how? I don't have access to the managedObject at this point.
This might be close the solution, but I can't figure out how to define 'oneWeek' and I don't think you can just add values when defining the predicate:
NSPredicate *pred = [NSPredicate predicateWithFormat:@"%@ < todoCompletedDate + %@", [NSDate date], oneWeek];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你快到了。
首先,您需要定义日期范围。为此,您需要从今天的日期开始,然后添加一周的天数以找到有效范围的末尾。一旦确定了该范围,您就可以构建谓词来查找截止日期 >= 开始且 <= 结束的所有任务。这是我编写的一些代码的摘录,用于执行非常类似的操作......
You're almost there.
Firstly you need to define your date range. To do that you'll want to start with today's date and then add a weeks worth of days to find the end of the valid range. Once you have that range you can build your predicate to find all tasks with a due date >= start and <= end. Here's an extract from some code I've written to do something very similar...
获取今天之前一周的数据。 swift代码
就我而言,我的 coreData DB 中有帖子的时间戳值,所以我也使用下面的函数来获取时间戳值
To get one week before data from today. swift code
In my case i have timestamp value of post in my coreData DB so i use below function also to get timestamp value
要获取一天的开始时间,您可以使用:
并获取结束日期:
To get the start of the day you could use:
and to get the end date: