2012-02-02 93 views
1

我从我的ManagedObject中得到了一个NSDate的数组,我想用一个表达式创建一个NSFetchRequest,这个表达式现在只有NSDate使用'now'的NSExpression函数

NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"eventDate"]; 
NSExpression *futureDateExpression = [NSExpression expressionForFunction:@"> now" arguments:[NSArray arrayWithObject:keyPathExpression]]; 

我想弄清楚如何编写这个函数。我试过其他变化,如SELF > now%@ > now,SELF > now()

所有这些给我SIGABRT

回答

2

1)您将需要NSPredicate。我不确定now()支持所有的后端。

3)但是你现在可以评估和替换它的参数:

NSPredicate *p = [NSPredicate predicateWithFormat:@"eventDate > %@", [NSDate date]]; 
[fetchRequest setPredicate:p]; 
+0

now()是一个有效的函数: http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSExpression_Class/Reference/NSExpression.html#//apple_ref/doc/uid/TP30001190-CJBDJCJE – 2012-02-02 21:24:09

+0

@DaveDeLong对,它只是可能不支持它的后端。更新。 – Krizz 2012-02-02 21:38:02

+0

谢谢你们。 3)解决了。我爱你和堆栈溢出。 – Lucien 2012-02-03 01:10:11

1

既然你在那里有一个比较,你没有一个NSExpression,而是一个NSPredicate

NSPredicate *p = [NSPredicate predicateWithFormat:@"eventDate > now()"]; 
[fetchRequest setPredicate:p]; 

事实证明,如果你使用上一个SQLite这个谓词-backed CoreData存储,您将无法使用now()。你可以做的反而是这(为suggested by @Krizz):

NSPredicate *p = [NSPredicate predicateWithFormat:@"eventDate > %@", [NSDate date]]; 
+0

感谢您的答复。我改变了代码,但现在这是错误:***终止应用程序由于未捕获的异常'NSInvalidArgumentException',原因:'不支持的函数表达式现在()' – Lucien 2012-02-02 20:14:04

+0

@Lucien更新回答 – 2012-02-02 21:25:20

相关问题