我正在尝试在我的CoreData实体食谱中准备多个搜索。我想准备一些参数。
食谱属性:
@property (nonatomic, retain) NSNumber * difficulty;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * code; //like identifier
@property (nonatomic, retain) NSNumber * prepTime;
成分是一个单独的实体,含有成分列表。
加入实体包含 ingredientCode , recipeCode ,计数。
getArrayOfJoinDataWithIngredients 获取加入实体,并返回包含某些输入成分的配方代码的NSArray。
这是我的代码:
- (IBAction)callRecipeFetch:(id)sender
{
NSString *predicateString = @"";
NSArray *codes = [[NSArray alloc] init]; codes = nil;
if ([paramController.ingredientsForSearchArray count] > 0) {
NSMutableArray *ingredientsArray = [[NSMutableArray alloc] init];
for (Ingredients *ingredient in paramController.ingredientsForSearchArray) {
[ingredientsArray addObject:ingredient.code];
}
MainTabController *mainTabController = [[MainTabController alloc] init];
codes = [mainTabController getArrayOfJoinDataWithIngredients:ingredientsArray];
NSString *ingrSet = [NSString stringWithFormat:@"(code IN %@)", codes];
predicateString = [predicateString stringByAppendingString:ingrSet];
}
NSString *diff;
if ([predicateString isEqualToString:@""]) {
diff = [NSString stringWithFormat:@"(difficulty <= %d)", paramController.diff.selectedSegmentIndex + 1];
}
else diff = [NSString stringWithFormat:@" AND (difficulty <= %d)", paramController.diff.selectedSegmentIndex + 1];
predicateString = [predicateString stringByAppendingString:diff];
NSString *timeString = [NSString stringWithFormat:@" AND (%d =< prepTime) AND (prepTime <= %d)", paramController.rangeSlider.leftValue, paramController.rangeSlider.rightValue];
predicateString = [predicateString stringByAppendingString:timeString];
if (paramController.categoryCode) {
NSString *categoryString = [NSString stringWithFormat:@" AND (inCategory = %@)", paramController.categoryCode];
predicateString = [predicateString stringByAppendingString:categoryString];
}
NSPredicate *predicate = [NSPredicate predicateWithFormat: predicateString];
[resultController findRecipesWithPredicate:predicate];
}
完整的predicateString是@"(code IN (\n 1,\n 3\n)) AND (difficulty <= 5) AND (0 =< prepTime) AND (prepTime <= 28800) AND (inCategory = 12)"
现在,当我使用代码准备NSPredicate时,我在谓词部分(代码IN%@)中出错:
NSPredicate *predicate = [NSPredicate predicateWithFormat: predicateString];
ERROR:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "(code IN (
1,
3
)) AND (difficulty <= 5) AND (0 =< prepTime) AND (prepTime <= 28800) AND (inCategory = 12)"'
如何使用 IN 运算符正确生成谓词。感谢您的所有建议。
答案 0 :(得分:40)
对您的多个谓词使用NSCompoundPredicate
,您可以参考NSCompoundPredicate Class Reference
类似的东西:
NSPredicate * andPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:predicate1,predicate2,predicate3,nil]];
答案 1 :(得分:12)
除了@Joshua的回答之外,您还可以使用NSCompoundPredicate进行这样的OR操作。
Obj-C - 或
// OR Condition //
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"X == 1"];
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"X == 2"];
NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[predicate1, predicate2]];
斯威夫特 - 或
let predicate1:NSPredicate = NSPredicate(format: "X == 1")
let predicate2:NSPredicate = NSPredicate(format: "Y == 2")
let predicate:NSPredicate = NSCompoundPredicate(orPredicateWithSubpredicates: [predicate1,predicate2] )
Swift 3 - 或
let predicate1 = NSPredicate(format: "X == 1")
let predicate2 = NSPredicate(format: "Y == 2")
let predicateCompound = NSCompoundPredicate.init(type: .or, subpredicates: [predicate1,predicate2])
答案 2 :(得分:0)
它适用于你。
NSPredicate *bPredicate;
bPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@ OR product_price contains[c] %@ OR foodescription contains[c] %@",searchText,searchText,searchText];
NSArray *filteredArray = [getObjectsFromServiceArray filteredArrayUsingPredicate:bPredicate];