Brian Weinreich
Brian Weinreich

Reputation: 7702

NSPredicate search with NSDate error

I'm attempting to get a list of to-do lists that have to-dos >= a certain date.

Data Model: Todolist has many Todos.

The due_at field in Todo model is a Date object.

This is the fetch request I'm using and it keeps crashing.

NSDate *now = [NSDate date];
int daysToAdd = _dueDateSlider.intValue;
NSDate *datePeriod = [now dateByAddingTimeInterval:60*60*24*daysToAdd];

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Todolist"];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"todos.due_at >= %@", datePeriod];
[request setPredicate:pred];
NSError *error = nil;
NSArray *todolists = [managedObjectContext executeFetchRequest:request error:&error];

The error I am receiving is:

-[__NSArrayI compare:]: unrecognized selector sent to instance 0x1018a4c50
An uncaught exception was raised

I'm not sure why this is happening, as any other predicate search works fine. todos.due_at != nil is fine as well as todos.content like '%test%' works fine.

Upvotes: 0

Views: 202

Answers (1)

Martin R
Martin R

Reputation: 539745

To get the lists that have any to-dos >= a certain date, use

 [NSPredicate predicateWithFormat:@"ANY todos.due_at >= %@", datePeriod];

Upvotes: 1

Related Questions