我有一个对象数组,它又有另一个对象数组(第二级),我的要求是根据第二级的值做一个过滤器。所以我使用嵌套的predicateWithBlock,但我没有得到所需的输出。嵌套的NSPredicate predicateWithBlock不工作
NSMutableArray *topLevelArray;
[topLevelArray filterUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
NSPredicate *pred = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
CustomType2 *obj2 = (CustomType2 *)evaluatedObject;
if ([obj2.name rangeOfString:searchText options:NSCaseInsensitiveSearch].location != NSNotFound)
{
return YES;
}
else
{
return NO;
}
}];
CustomType1 *obj1 = (CustomType1 *)evaluatedObject;
[obj1.customType2 filteredSetUsingPredicate:pred];
return YES;
}]];
输入:
topLevelArray = {
[CustomType1.name = "Alpha",
CustomType1.customType2 = {
[CustomType2.name = "Apple"],
[CustomType2.name = "Banana"],
[CustomType2.name = "Cocoplum"]}
],
[CustomType1.name = "Beta",
CustomType1.customType2 = {
[CustomType2.name = "Asparagus"],
[CustomType2.name = "Broccoli"]}
],
[CustomType1.name = "Gama",
CustomType1.customType2 = {
[CustomType2.name = "Aligator"]}
]};
输出:
searchText = @"Bro";
topLevelArray = {
[CustomType1.name = "Alpha",
CustomType1.customType2 = {
[CustomType2.name = "Apple"],
[CustomType2.name = "Banana"],
[CustomType2.name = "Cocoplum"]}
],
[CustomType1.name = "Beta",
CustomType1.customType2 = {
[CustomType2.name = "Asparagus"],
[CustomType2.name = "Broccoli"]}
],
[CustomType1.name = "Gama",
CustomType1.customType2 = {
[CustomType2.name = "Aligator"]}
]};
预期输出:
searchText = @"Bro";
topLevelArray = {
[CustomType1.name = "Alpha",
CustomType1.customType2 = {nil}
],
[CustomType1.name = "Beta",
CustomType1.customType2 = {
[CustomType2.name = "Broccoli"]}
],
[CustomType1.name = "Gama",
CustomType1.customType2 = {nil}
]};
注意'[filteredSetUsingPredicate:]'和'[filterUsingPredicate:]' – borrrden 2014-10-09 04:18:31
'之间的区别'if(something)return YES;否则返回NO;'哦不...'为什么你不能'返回一些东西;' – 2014-10-09 04:35:41
你需要搜索customtype2对象中的数据,我是对的。 – 2014-10-09 04:52:15