【问题标题】:NSPredicate stringNSPredicate 字符串
【发布时间】:2013-07-30 14:31:08
【问题描述】:

我有一个NSPredicate 用于搜索Core Data

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"objID == %@ AND createDate >= %@ AND createDate <= %@",self.objID, self.fromDate, self.toDate];

我想扩展此谓词以包含日期范围,或者如果“createDate”为 nil。我对 SQL 类型搜索不太熟悉。

基本上,我想找到所有 Core Data 具有 objID 且在日期范围内或为零的实体。

if (objID == X && ((dateRange >= fromDate && dateRange <= toDate) || dateRange == nil))

我该怎么做?

【问题讨论】:

  • 你试过把它翻译成谓词吗?

标签: ios objective-c core-data nspredicate


【解决方案1】:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"objID == %@ AND (createDate >= %@ AND createDate <= %@ OR createDate = nil)",self.objID, self.fromDate, self.toDate];

我刚刚添加了OR createDate = nil

检查this 答案。 Christopher Pickslay 说,你可以使用“nil”或“NULL”,但不能使用“NIL”。

【讨论】:

  • 我不需要像在 If stmt 中那样将它们分组?
  • @Log139,NSPredicate 根据逻辑代数对语句进行分组。 A AND B OR C 等于 (A AND B) OR C
  • 好的,这个谓词分组是像(objID = X AND createDate &gt;= X AND createDate &lt;= X) OR createDate = nil吗?
  • @Log139,是的。你可以吗?如果没有,只需在谓词的主体中添加括号
  • 好的,谢谢。不,我需要它像 objID == X AND ((AND createDate &gt;= X AND createDate &lt;= X) OR create = nil) 一样 () 在谓词中工作吗?
猜你喜欢
  • 2014-09-14
  • 1970-01-01
  • 2019-07-25
  • 2014-11-12
  • 1970-01-01
  • 2013-01-14
  • 2011-03-25
  • 1970-01-01
相关资源
最近更新 更多