使用字典属性过滤自定义对象的数组

时间:2014-12-16 12:41:48

标签: objective-c filter

我有一个包含MyCustomObject对象的数组。

MyCustomObject有3个属性:

NSString *id;
NSString *name;
NSDictionary *phones;

如何根据"手机"的内容过滤该阵列?属性?

我在网上看到的只有:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"phones CONTAINS[c] %@",textField.text];
self.filteredArray = [self.unfilteredArray filteredArrayUsingPredicate:predicate];

但它对我帮助不大......

由于

2 个答案:

答案 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 );

  }]];