当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - 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.

网友答案:

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

 [NSPredicate predicateWithFormat:@"ANY todos.due_at >= %@", datePeriod];
分享给朋友:
您可能感兴趣的文章:
随机阅读: