我有一个来自ManagedObject的NSDate数组,我想使用一个表达式创建一个NSFetchRequest,这个表达式在以后只给我NSDate,然后现在。
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"eventDate"];
NSExpression *futureDateExpression = [NSExpression expressionForFunction:@"> now" arguments:[NSArray arrayWithObject:keyPathExpression]];我正在尝试弄清楚如何编写这个函数。我尝试过其他变体,如SELF > now或%@ > now、SELF > now()
所有这些都给了我SIGABRT。
发布于 2012-02-03 05:20:22
1)您将需要NSPredicate。
2)我不确定所有的后端都支持now()。
3)但是您可以现在求值并将其替换为参数:
NSPredicate *p = [NSPredicate predicateWithFormat:@"eventDate > %@", [NSDate date]];
[fetchRequest setPredicate:p];发布于 2012-02-03 03:56:46
因为您有一个比较,所以您没有NSExpression,而是NSPredicate
NSPredicate *p = [NSPredicate predicateWithFormat:@"eventDate > now()"];
[fetchRequest setPredicate:p];事实证明,如果您在SQLite支持的CoreData存储上使用这个谓词,就不能使用now()。相反,您可以执行以下操作(以suggested by @Krizz身份):
NSPredicate *p = [NSPredicate predicateWithFormat:@"eventDate > %@", [NSDate date]];https://stackoverflow.com/questions/9118651
复制相似问题