我有一个模型,其中包含多个UpcomingDates的事件,每个事件的属性日期都为(NS)Date。我正在构建一个获取请求,以仅检索在两个给定日期之间具有即将到来的日期的事件。问题是,无论我如何格式化谓词中的输入日期,我都会遇到异常。看起来Core Data希望我的谓词参数既是NSDates又是NSNumbers。
当我通过@([date timeIntervalSinceReferenceDate])传入NSNumbers时,我看到了:
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
打印出接收无法识别的选择器的对象,它始终是谓词参数的第一个。
我已尽最大努力验证即将发布的Date.date实际上是一个NSDate(这是模型指定的内容,但我也看了一下创建时传入的内容)。我已经尝试过对NSDates和NSNumbers使用CAST的每个排列。我已经将谓词分解为最简单的情况,这实际上只是意味着删除了一个ANDed子查询谓词。我很难过。
ETA: 谓词如下。 NSNumbers:
[NSPredicate predicateWithFormat:@"0 != (SUBQUERY(upcomingDates, $upcomingDate, ($upcomingDate.date >= %@) AND ($upcomingDate.date <= %@))).@count", @([self.timeFilterRange[0] timeIntervalSinceReferenceDate]), @([self.timeFilterRange[1] timeIntervalSinceReferenceDate])]
NSDates:
[NSPredicate predicateWithFormat:@"0 != (SUBQUERY(upcomingDates, $upcomingDate, ($upcomingDate.date >= %@) AND ($upcomingDate.date <= %@))).@count", self.timeFilterRange[0] , self.timeFilterRange[1] ]
答案 0 :(得分:0)
NSArray *times = @[@([self.timeFilterRange[0] timeIntervalSinceReferenceDate]),
@([self.timeFilterRange[1] timeIntervalSinceReferenceDate])];
[NSPredicate predicateWithFormat:@"ANY upcomingDates.date BETWEEN %@", times]