DATEADD的NSPredicate语法? [英] NSPredicate syntax for DATEADD?

查看:187
本文介绍了DATEADD的NSPredicate语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有办法在NSPredicate上做DateAdd或DateDiff函数吗?
谢谢你,
Jose。

解决方案

这是一个迂回的做法,因为 NSPredicate 不直接支持它(即,你不能只是 + anInterval NSDate )。幸运的是,你可以做到,幸运的是,我只是想了大约2天前。



澄清:我假设你要的东西我想看看这个 date 属性是一个任意的时间间隔之前/后另一个日期。如果这是你的意思,那么是的,你可以做到。如果这不是你的意思,那么请澄清你的问题。



这里我们去。在这个例子中,我们将假设我们将评估这个谓词的对象有一个名为 date 的属性,它返回一个 NSDate 。我们将看看这个日期是否至少比另一个日期早1天。有几种不同的方式,我们可以做这个比较:

 日期+ 1天在比较日期之前
日期之前compareDate - 1天$ ​​b $ b

我将使用第一种方法:

  NSDate * comparisonDate = ...; //一个任意的NSDate对象,我们将对其进行比较
NSTimeInterval interval = 86400; //您想要添加或减少的秒数。

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

发生了什么:



这个工作,我们 CAST()一个日期对象到 NSNumber 。这将把 NSDate 转换为 NSNumber ,这是从参考时间点开始的一些秒数1970年1月1日或任何)。对于那个数字,我们加上或减去我们的间隔,然后将新的数字转换回 NSDate 。然后我们可以使用常规比较运算符来比较我们的新日期与我们的比较日期。



这个想法有几个不同的变化,但他们都需要转换一个 NSDate 到一个数字(或者将一个数字转换为 NSDate )以获取其时间间隔。



猥亵,嗯?



编辑 >如果Core Data抱怨谓词的构造,尝试颠倒以下事情:

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

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

编辑#2



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


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

解决方案

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

这篇关于DATEADD的NSPredicate语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆