Reputation: 422
I have a Word of a Day app. I store the date of the word with NSSTRING
and all is working fine. But when i want to show all words in the database, which date is less than [NSDate date]
- which means today, it doesn't sort it well. I'm using NSpredicate to do that. How to make it work?
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"date <= %@", [NSDate date]];
[fetchRequest setPredicate:predicate];
Even when i put the string from current date, it doesn't work. I mean the comparison is only work for current month, because the format is : 11.06.2014
Upvotes: 0
Views: 69
Reputation: 170839
The best solution would be to store your date as NSDate or a timestamp in database and convert it to NSString only for display.
If you cannot change your database scheme you can create predicate with +predicateWithBlock method:
NSPredicate *predicate = [NSPredicate predicateWithBlock: ^BOOL(YourManagetObj* obj, NSDictionary *bind){
NSDate* objDate = ... //convert string to date with the same date formatter you used to convert date to string
return [objDate earlierDate:[NSDate date]] == objDate;
}];
Note also that with your scheme you'll have to use custom predicate that converts stored string to NSDate for sorting your objects by date.
Upvotes: 2