核心数据ANY BETWEEN谓词 [英] Core Data ANY BETWEEN predicate

查看:223
本文介绍了核心数据ANY BETWEEN谓词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个NSPredicate以查找在某个日期范围内包含会话的项目。我一开始尝试过:

  [NSPredicate predicateWithFormat:@ANY sessions.date BETWEEN {$ STARTDATE,$ ENDDATE}] ; 

但我遇到了一个例外:

  ***由于未捕获异常NSInvalidArgumentException而终止应用程序,原因:
'to-many key not allowed here'
pre>

看起来这不会以这种方式与ANY一起工作。我也限制在使用()和AND子句意味着我不能使用像:

  [NSPredicate predicateWithFormat: @ANY(sessions.date> $ STARTDATE&& amp; sessions.date< $ ENDDATE)]; 

如果我尝试,我得到一个解析错误。当然session.date真的是一个集合,所以ANDing它像这样不真正有很大的意义。



我该怎么做?



感谢



更新:请注意:

  [NSPredicate predicateWithFormat:@ANY sessions.date> $ STARTDATE&& ANY sessions.date< $ ENDDATE]; 

不正确,因为它返回一个项目,其中有一个会话大于开始日期,

解决方案

我认为这是一个需要 SUBQUERY 表达式:

  [NSPredicate predicateWithFormat:@SUBQUERY(sessions,$ s,$ s.date BETWEEN {$ STARTDATE,$ ENDDATE})。@ count> 0]; 


I'm trying to create an NSPredicate to find 'projects' that contain 'sessions' within a certain date range. I tried this at first:

[NSPredicate predicateWithFormat:@"ANY sessions.date BETWEEN {$STARTDATE, $ENDDATE}"];

But I get an exception:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
   'to-many key not allowed here'

It seems BETWEEN does not work with ANY in this way. I'm also limited in use of () and AND clauses meaning I can't use something like:

[NSPredicate predicateWithFormat:@"ANY (sessions.date > $STARTDATE && sessions.date < $ENDDATE)"];

If I try that I get a parse error. And of course sessions.date is really a set so ANDing it like that doesn't really make a lot of sense.

How can I do this?

Thanks

UPDATE: Note that this:

[NSPredicate predicateWithFormat:@"ANY sessions.date > $STARTDATE && ANY sessions.date < $ENDDATE"];

Is incorrect because it returns a project where there is a session greater than the start date and another session less than the end date but no session in between.

解决方案

I think this is a case where you need a SUBQUERY expression:

[NSPredicate predicateWithFormat:@"SUBQUERY(sessions, $s, $s.date BETWEEN {$STARTDATE, $ENDDATE}).@count > 0"];

这篇关于核心数据ANY BETWEEN谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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