2013-05-08 56 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

相关问题