NSPredicate BETWEEN with NSDate causes -[__NSDate constantValue]: unrecognized selector sent to instance 0x1e7ff0

前端 未结 2 839
野性不改
野性不改 2020-12-11 03:23

I\'m trying to fetch from Core Data records that have a startTime between two dates.

Here\'s my code:

NSDate *today = [NSDate date];
NSDate *distantF         


        
相关标签:
2条回答
  • 2020-12-11 03:43

    Perhaps try using AND instead of && (although your console output seems to show it converting correctly.

    I just posted an example that works for me, note that my condition is flipped like so:

    @"(%K >= %@) AND (%K <= %@)"

    Here is the full example

    0 讨论(0)
  • 2020-12-11 03:57

    According to the documentation, the BETWEEN operator counts as an aggregate expression, and ...

    Aggregate expressions are not supported by Core Data.

    So yes, in order to work around this, you must use the >= and <= construction.

    However, given that one of your dates is [NSDate distantFuture], you don't need the "less than" comparison at all. You should be able to do:

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"startTime >= %@", [NSDate date]];
    
    0 讨论(0)
提交回复
热议问题