Duncan Groenewald
Duncan Groenewald

Reputation: 8988

Should this date predicate work?

Is there any reason this search predicate should not work on iOS ? The same seems to work fine on OS X. I checked and date.description returns a string in this format '2013-08-28 23:18:16 +0000'. I am trying to implement a UISearchBar to filter results and would like the user to be able to type in any part of the date.

[predicates addObject:[NSPredicate predicateWithFormat:@"date.description contains %@", self.searchString]];

Thanks

Upvotes: 1

Views: 454

Answers (1)

Matthias Bauch
Matthias Bauch

Reputation: 90117

No. If it works it's coincidence. According to the documentation description of NSDate is for debugging purposes only.

Turn your NSString into a NSDate and write your predicate like this:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
df.dateFormat = @"yyyy-MM-dd HH:mm:ss ZZZ";

NSDate *date = [df dateFromString:self.searchString];
NSPredicate *p = [NSPredicate predicateWithFormat:@"date == %@", date];

Edit: After reading your question again this answer is probably not very helpful.
I would try to save the result of [df stringFromDate:date]; for each object somewhere. Then you should be able filter with a contains predicate. But you must not rely on description.

Upvotes: 1

Related Questions