2012-02-02 86 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

相關問題