13

如何使用多个条件NSPredicate

我正在使用它,但在返回的数组中没有得到任何东西。

NSPredicate *placePredicate = [NSPredicate predicateWithFormat:@"place CONTAINS[cd] %@ AND category CONTAINS[cd] %@ AND ((dates >= %@) AND (dates <= %@)) AND ((amount >= %f) AND (amount <= %f))",placeTextField.text,selectedCategory,selectedFromDate,selectedToDate,[amountFromTextField.text floatValue],[amountToTextField.text floatValue]];

NSArray *placePredicateArray = [dataArray filteredArrayUsingPredicate:placePredicate];

NSLog(@"placePredicateArray %@", placePredicateArray);

金额和类别有时可能为空。我应该如何构建NSPredicate

4

3 回答 3

30

您可以placesPredicate使用其他NSPredicate对象和NSCompoundPredicate类来构建您的

就像是 :

NSPredicate *p1 = [NSPredicate predicateWithFormat:@"place CONTAINS[cd] %@", placeTextField.text];
NSPredicate *p2 = [NSPredicate predicateWithFormat:@"category CONTAINS[cd] %@", selectedCategory];
NSPredicate *p3 = [NSPredicate predicateWithFormat:@"(dates >= %@) AND (dates <= %@)", selectedFromDate,selectedToDate];
NSPredicate *p4 = [NSPredicate predicateWithFormat:@"(amount >= %f) AND (amount <= %f)", [amountFromTextField.text floatValue],[amountToTextField.text floatValue]]; 

NSPredicate *placesPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[p1, p2, p3, p4]];

现在,如果您缺少类别,例如,您可以使用虚拟YES谓词来替换它:

NSPredicate *p2;
if (selectedCategory) {
    p2 = [NSPredicate predicateWithFormat:@"category CONTAINS[cd] %@", selectedCategory];
} else {
    p2 = [NSPredicate predicateWithBool:YES]
}
于 2012-04-13T12:30:14.353 回答
13

我倾向于零碎地处理这件事。那是,

placePredicate = [NSPredicate predicateWithFormat:@"place CONTAINS[cd] %@",placeTextField.text];
NSMutableArray *compoundPredicateArray = [ NSMutableArray arrayWithObject: placePredicate ]; 

if( selectedCategory != nil ) // or however you need to test for an empty category
    {
    categoryPredicate = [NSPredicate predicateWithFormat:@"category CONTAINS[cd] %@",selectedCategory];
    [ compoundPredicateArray addObject: categoryPredicate ];
    }

// and similarly for the other elements.  

请注意,当我知道没有谓词时,我什至不费心将类别(或其他任何东西)的谓词放入数组中。

// Then
    NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:
                                  compoundPredicateArray ];

如果我打算做很多,我不会使用格式方法,而是围绕构建块,只是改变使用之间的任何变化。

于 2012-04-13T12:35:08.487 回答
0
Suppose a class Person with attributes "name", "age", "income"

"personArray" is array of class Person

NSPredicate *mypredicate = [NSPredicate predicateWithBlock:^BOOL(personObj Person, NSDictionary *bindings) {
     NSNumber *age = personObj.age;
     String *name = personObj.name;
     NSNumber *income = personObj.income
     BOOL result = (age > 20 && name == "Stack" && income >40000);
     return result;
}];

NSArray *filteredArray = [personArray filteredArrayUsingPredicate: mypredicate];

进一步你可以阅读这篇文章Array Filter Using Blocks

在斯威夫特

let filterArray = personArray.filter
            { person in
           let age = personObj.age;
           let name = personObj.name;
           let income = personObj.income
           BOOL result = (age > 20 && name == "Stack" && income >40000);
          return result;
        }
于 2016-02-11T11:14:39.560 回答