DATEADD 的 NSPredicate 语法?

发布于 2024-10-02 09:23:03 字数 61 浏览 7 评论 0原文

有没有办法在 NSPredicate 上执行 DateAdd 或 DateDiff 函数? 谢谢你, 何塞.

is there a way to do a DateAdd or a DateDiff function on an NSPredicate?
Thank you,
Jose.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

哆啦不做梦 2024-10-09 09:23:03

事实上,有!这是一种迂回的方式,因为 NSPredicate 不直接支持它(即,你不能只是 + anIntervalNSDate )。幸运的是,你可以做到,而且幸运的是,我大约两天前才想出来。

澄清一下:我假设您要求类似:“一个对象有一个 date 属性。我想看看这个 date 属性是否是之前的某个任意间隔/另一个日期之后”。如果这就是您的意思,那么您可以做到。如果这不是您的意思,那么请澄清您的问题。

开始了。在此示例中,我们将假设要评估此谓词的对象有一个名为 date 的属性,该属性返回 NSDate。我们将查看此日期是否比另一个日期至少早 1 天。我们可以通过几种不同的方法进行比较:

date + 1 day is before comparisonDate
date is before comparisonDate - 1 day

我将采用第一种方法:

NSDate * comparisonDate = ...; //an arbitrary NSDate object, against which we're going to be doing our comparison
NSTimeInterval interval = 86400; //the number of seconds you want add or subtract.

NSPredicate * p = [NSPredicate predicateWithFormat:@"CAST(CAST(date, 'NSNumber') + %f, 'NSDate') < %@", interval, comparisonDate];

发生了什么:

为了使其正常工作,我们可以将日期对象CAST() NSNumber。这会将 NSDate 转换为 NSNumber,它是距参考时间点(例如 1970 年 1 月 1 日或其他)的秒数。我们对这个数字添加或减去我们的间隔,然后将新数字转换回 NSDate。然后我们可以使用常规比较运算符将新日期与比较日期进行比较。

这个想法有几个不同的变体,但它们都需要将 NSDate 转换为数字(或将数字转换为 NSDate)来获取其时间间隔。

狡猾的,嗯?

编辑

如果核心数据抱怨谓词的构造,请尝试逆转事情:

NSDate * comparisonDate = ...;
NSTimeInterval interval = 86400;

NSPredicate * p = [NSPredicate predicateWithFormat:@"date < CAST(CAST(%@, 'NSNumber') - %f, 'NSDate')", comparisonDate, interval];

编辑#2

牧师偷走的雷霆:http://tumblr.com/xqorqjfrz

Actually, there is! It's a roundabout way of doing it, because NSPredicate doesn't support it directly (ie, you can't just + anInterval to an NSDate). Fortunately, you can do it, and luckily for you, I just figured it out about 2 days ago.

To clarify: I'm assuming you're asking for something like: "an object has a date property. I want to see if this date property is some arbitrary interval before/after another date". If that's what you mean, then yes you can do it. If that's not what you mean, then please clarify your question.

Here we go. In this example, we're going to assume the object against which we'll be evaluating this predicate has a property called date that returns an NSDate. We are going to see if this date is at least 1 day before another date. There are a couple different ways we could do this comparison:

date + 1 day is before comparisonDate
date is before comparisonDate - 1 day

I'm going to go with the first approach:

NSDate * comparisonDate = ...; //an arbitrary NSDate object, against which we're going to be doing our comparison
NSTimeInterval interval = 86400; //the number of seconds you want add or subtract.

NSPredicate * p = [NSPredicate predicateWithFormat:@"CAST(CAST(date, 'NSNumber') + %f, 'NSDate') < %@", interval, comparisonDate];

What's going on:

To get this to work, we can CAST() a date object to an NSNumber. This is going to convert the NSDate into an NSNumber which is some number of seconds from a reference point in time (such as 1 Jan 1970 or whatever). To that number we add or subtract our interval, and then cast the new number back to an NSDate. Then we can use a regular comparison operator to compare our new date against our comparison date.

There are a couple different variations on this idea, but they all require casting an NSDate to a number (or casting a number to an NSDate) to get its time interval.

Devious, eh?

edit

If Core Data is complaining about the construct of the predicate, try reversing things:

NSDate * comparisonDate = ...;
NSTimeInterval interval = 86400;

NSPredicate * p = [NSPredicate predicateWithFormat:@"date < CAST(CAST(%@, 'NSNumber') - %f, 'NSDate')", comparisonDate, interval];

edit #2

The thunder that The Reverend stole: http://tumblr.com/xqorqjfrz

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