2

我有一个带有多个 UpcomingDates 的事件模型,每个事件都有一个类型为 (NS)Date 的属性日期。我正在构建一个获取请求以仅检索在两个给定日期之间具有即将到来的日期的事件。问题是,无论我如何格式化谓词中的输入日期,我都会遇到异常。似乎 Core Data 希望我的谓词参数既是 NSDates 又是 NSNumbers。

当我传入 NSNumbers 时,通过执行 @([date timeIntervalSinceReferenceDate]),我看到了:

predicate: 0 != SUBQUERY(upcomingDates, $upcomingDate, $upcomingDate.date >= 387270000 AND $upcomingDate.date <= 387356400).@count
-[__NSCFNumber timeIntervalSinceReferenceDate]: unrecognized selector sent to instance 0x865a0b0

当我走直观的路线时,使用普通的旧 NSDates,我看到了:

predicate: 0 != SUBQUERY(upcomingDates, $upcomingDate, $upcomingDate.date >= CAST(387270000.000000, "NSDate") AND $upcomingDate.date <= CAST(387356400.000000, "NSDate")).@count
-[__NSDate objCType]: unrecognized selector sent to instance 0x86c6450

打印出接收到无法识别的选择器的对象,它始终是谓词参数中的第一个。

我已经尽我所能验证了comingingDate.date 实际上是一个NSDate(这是模型指定的,但我也查看了它们创建时传入的内容)。我已经尝试使用 CAST 到 NSDates 和 NSNumbers 的每一个排列。我已将谓词分解为最简单的情况,这实际上只是意味着删除一个 AND 子查询谓词。我难住了。

ETA:谓词如下。NSNumbers:

[NSPredicate predicateWithFormat:@"0 != (SUBQUERY(upcomingDates, $upcomingDate, ($upcomingDate.date >= %@) AND ($upcomingDate.date <= %@))).@count", @([self.timeFilterRange[0] timeIntervalSinceReferenceDate]), @([self.timeFilterRange[1] timeIntervalSinceReferenceDate])]

NSD日期:

[NSPredicate predicateWithFormat:@"0 != (SUBQUERY(upcomingDates, $upcomingDate, ($upcomingDate.date >= %@) AND ($upcomingDate.date <= %@))).@count", self.timeFilterRange[0] , self.timeFilterRange[1] ]
4

1 回答 1

0

Weirdly, it seems the subquery was the problem. When I use this new predicate, I may or may not be getting correct results (my setup is such that it's difficult to test right now), but I am at least not getting crashes.

NSArray *times = @[@([self.timeFilterRange[0] timeIntervalSinceReferenceDate]),
                       @([self.timeFilterRange[1] timeIntervalSinceReferenceDate])];
[NSPredicate predicateWithFormat:@"ANY upcomingDates.date BETWEEN %@", times]
于 2013-04-13T22:36:50.453 回答