我有一个包含MyCustomObject对象的数组。
MyCustomObject有3个属性:
NSString *id;
NSString *name;
NSDictionary *phones;
如何根据"手机"的内容过滤该阵列?属性?
我在网上看到的只有:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"phones CONTAINS[c] %@",textField.text];
self.filteredArray = [self.unfilteredArray filteredArrayUsingPredicate:predicate];
但它对我帮助不大......
由于
答案 0 :(得分:5)
MyCustomObject *value = [[MyCustomObject alloc] init];
for(value in arrayname)
{
NSString *str = [value.phones objectForKey:@"key"];
NSRange r = [str rangeOfString:textField.text options:NSCaseInsensitiveSearch];
if(r.location != NSNotFound)
{
NSLog(@"Match found");
}
}
答案 1 :(得分:0)
self.filterArray = [self.unfilteredArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
MyCustomObject *obj = (MyCustomObject*)evaluatedObject;
return ([[[obj.phones objectForKey:@"key"] lowercaseString] rangeOfString:[textField.text lowercaseString]].location != NSNotFound );
}]];