CoreData NSPredicate按日期获取数据

时间:2015-01-29 10:38:17

标签: ios iphone core-data nspredicate

请帮我制作这个谓词

[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"subsidiary_of_cat_id == %@ AND (valid_from < %@) AND (valid_till > %@ OR valid_till == %@) ",catId,[NSDate date],[NSDate date], dateTo,]]

收到崩溃错误:无法解析格式字符串

从CoreData获取数据的条件

条件1:now()&lt; valid_till或now()= [NSDate date]
条件2:now()&gt; VALID_FROM

谢谢!

已解决:更新了代码

NSMutableArray *predicatesArray = [NSMutableArray array];
    [predicatesArray addObject:[NSPredicate predicateWithFormat:@"%@ == subsidiary_of_cat_id",catId]];
    [predicatesArray addObject:[NSPredicate predicateWithFormat:@"%@ > valid_from",[NSDate date]]];
    [predicatesArray addObject:[NSPredicate predicateWithFormat:@"%@ < valid_till",[NSDate date]]];
    [predicatesArray addObject:[NSPredicate predicateWithFormat:@"%@ == valid_till”,dateTo];
[NSCompoundPredicate orPredicateWithSubpredicates:predicatesArray]

1 个答案:

答案 0 :(得分:2)

您无需将字符串包装在stringWithFormat

[NSPredicate predicateWithFormat:@"subsidiary_of_cat_id == %@ AND (valid_from < %@) AND (valid_till > %@ OR valid_till == %@) ",catId,[NSDate date],[NSDate date], dateTo];