【问题标题】:NSPredicate with IN for NSDateNSPredicate 和 IN 用于 NSDate
【发布时间】:2013-07-15 06:46:42
【问题描述】:

我想从startDateendDate 大于日期的实体中获取对象 以数组形式提供。

我目前使用 IN 作为谓词如下,

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K IN %@ OR %K in %@", kEntityEventAttributeStartDate, inArray, kEntityEventAttributeEndDate, inArray];
request setPredicate:predicate];

但我认为它会检查相同的日期,因为我想要更大的日期。

任何想法如何使用NSPredicate 实现这一目标。

提前致谢。

【问题讨论】:

  • “startDate 大于数组中的日期”是什么意思?它应该大于数组中的所有值吗?
  • 为什么要检查startDateendDate 是否必须大于所有日期?您不能只取最大的日期并使用%K > %@ 与它进行比较吗?
  • 不是所有的值,但应该大于至少一个值
  • 即使在这种情况下,您也只需要检查给定日期是否大于数组中的最小日期。
  • 我按照你建议的方式(Rakesh 和 PartiallyFinite)我现在得到了对象。谢谢

标签: objective-c nsdate nspredicate


【解决方案1】:

您可以先对日期数组进行排序,如:

NSArray *dateArraySorted = [inArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"Date" ascending:YES]]];

//Select the smallest date - if the date is greater than this then there is no point in checking against others and vice-versa.
NSDate *smallestDate = dateArraySorted[0];

从这里,我猜你知道如何创建谓词了。

【讨论】:

    【解决方案2】:

    我通过创建一个具有开始日期和结束日期的谓词来解决它,我查询模型以获取在提供的范围之间的所有日期。

    感谢您的帮助和建议。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-19
      • 2011-09-13
      • 2014-03-21
      • 2012-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多