2013-05-08 104 views
1

我構建了以下謂詞以針對Core Data存儲使用MagicalRecord。爲什麼我得到「無法解析NSPredicate」錯誤

// format the date correctly 
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init]; 
[dateFormat setDateFormat:@"yyyy-MM-dd*"]; // HH:mm 
NSString *formattedDate = [dateFormat stringFromDate:currentlySelectedDate]; 

NSString *stringPredicate = [NSMutableString stringWithFormat: 
          @"aPosX >= %f AND aPosX < %f AND %f > aPosY AND %f <= (aPosY + aPosH) AND aStartTime LIKE %@", 
          [staffIndex floatValue], [staffIndex floatValue] + 218, touchPoint.y, touchPoint.y, formattedDate]; 

NSPredicate *predicate = ([NSPredicate predicateWithFormat: stringPredicate]); 

它崩潰的最後一條語句,此錯誤:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "aPosX >= 218.000000 AND aPosX < 436.000000 AND 15.000000 > aPosY AND 15.000000 <= (aPosY + aPosH) AND aStartTime LIKE 2013-05-09*"'

我都試過,我知道的一切,搜索谷歌和SO,仍然不明白爲什麼它的失敗...有人請賜教嗎?

回答

3

不要創建一個格式字符串,使用謂詞格式創建方法:

predicateWithFormat: 

這使得謂詞,因爲它認爲合適的方式處理格式參數(如需要加引號)。

對於日期,您需要修改謂詞以使用範圍。你不能直接只檢查日期的天而忽略了時間,所以你需要說:

date > startOfDay AND date < endOfDay 

startOfDayendOfDay是NSDate的實例(使用NSDateComponents可能創建)。

+0

謝謝......它不再崩潰,但謂詞不按我期望的方式工作......我有一個日期時間存儲在** astartTime **中,它看起來在看整個日期/時間。我只需要**日期**,而不是時間......我如何才能做到這一點? – SpokaneDude 2013-05-09 03:18:44

+0

非常感謝你......那麼做了...... – SpokaneDude 2013-05-09 15:24:48

相關問題