SandeepAggarwal
SandeepAggarwal

Reputation: 1293

Date Comparison NSPredicate

Case 1:

[NSPredicate predicateWithFormat:@" end_time < '2015-07-27 06:22:43 +0000'"]

crashed

reason: '-[__NSCFString timeIntervalSinceReferenceDate]: unrecognized selector

Case 2:

[NSPredicate predicateWithFormat:@" end_time < 2015-07-27 06:22:43 +0000"]

crashed

 reason: 'Unable to parse the format string " end_time < 2015-07-27 06:22:43 +0000"'

Case 3:

[NSPredicate predicateWithFormat:@" end_time < %@",[NSDate date]

works!

Can someone explain me the difference between the three.

Upvotes: 0

Views: 616

Answers (1)

avismara
avismara

Reputation: 5149

First case:

endTime < '2015-07-27 06:22:43 +0000'. I'm sure that the data type of endTime is NSDate. The runtime tried to treat the rhs as a date object as well and tried to compare. But it is a string. Crash.

Second case:

This is simply the wrong syntax. There is a whitespace after 2015-07-27. Runtime doesn't know how to parse this. Crash.

(Even if your predicate was, [NSPredicate predicateWithFormat:@"end_time < 2015-07-27"], it would be a valid predicate format. But would crash because of unrecognized selector on NSNumber? I am not sure.)

Third case:

rhs is a date object. lhs is a date object. timeIntervalSinceReferenceDate is called on an NSDate instance. Works.

Upvotes: 2

Related Questions