17

我正在尝试在我的 CoreData 实体Recipes中准备多个搜索。我想通过一些参数来准备获取。

食谱属性:

@property (nonatomic, retain) NSNumber * difficulty;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * code; //like identifier
@property (nonatomic, retain) NSNumber * prepTime;

成分是具有成分列表的单独实体。

连接实体包含成分代码、配方代码计数

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];
}

完整的谓词字符串是@"(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];

错误:

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运算符进行谓词。感谢所有建议。

4

3 回答 3

41

用于NSCompoundPredicate您的多个谓词,您可以参考NSCompoundPredicate 类参考

像这样的东西:

NSPredicate * andPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:predicate1,predicate2,predicate3,nil]];
于 2013-05-20T05:11:55.397 回答
13

除了@Joshua 的回答,您还可以像这样使用 NSCompoundPredicate 进行 OR 操作。

对象-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] )

斯威夫特 3 - 或

    let predicate1 = NSPredicate(format: "X == 1")
    let predicate2 = NSPredicate(format: "Y == 2")
    let predicateCompound = NSCompoundPredicate.init(type: .or, subpredicates: [predicate1,predicate2])
于 2015-12-10T23:17:46.397 回答
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];
于 2016-08-25T10:18:10.573 回答