1
votes

Please help me to make this predicate

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

receive crash error: Unable to parse the format string

Conditions to fetch data from CoreData

Condition 1: now() < valid_till OR now() = [NSDate date]
Condition 2: now() > valid_from

Thanks!

RESOLVED: Updated code

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
I had resolved it by doing [NSCompoundPredicate orPredicateWithSubpredicates:predicatesArray] - Karamjeet Singh

1 Answers

2
votes

You don't need to wrap the string in stringWithFormat:

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