使用多键+ NSpredicate搜索嵌套数组

时间:2015-12-12 10:56:06

标签: ios

我有一个

(
  {
    ContactID = 100;
    Emails =     (

    );
    FirstName = TestName;
    FullName = “Test Name Test Name“;
    LastName = Testing LastName;
    PhoneNumbers =     (
    "1234-567890"
    );
    type = PhoneBook;
  }
)

- 我想使用NSPredicate从上面的数组中搜索FullName,PhoneNumbers和Emails,phonenumbers和电子邮件包含数组,所以我从嵌套数组中搜索值,给我建议如何使用这种格式搜索值。

1 个答案:

答案 0 :(得分:0)

试试这个。

   NSString *str=[NSString stringWithFormat:@"FullName beginswith[c] '%@' OR FirstName beginswith[c] '%@' OR LastName beginswith[c]'%@'",searchString,searchString,searchString];

            NSPredicate *bPredicate = [NSPredicate predicateWithFormat:str];
            arraySearchResult = [arrayConatcts filteredArrayUsingPredicate:bPredicate];
                                                     // allocate an empty mutable array in which to accumulate the matches
            NSString *str1=[NSString stringWithFormat:@"self beginswith[c]'%@'",searchString];
               NSPredicate *Predicate = [NSPredicate predicateWithFormat: str1];
             NSMutableArray *test = [NSMutableArray new];
            for(FreindsContact *obj in arrayConatcts)
            {// loop over each sub array
                 NSMutableArray *filtered = [NSMutableArray new];
                [filtered addObjectsFromArray:[obj.ContactNoArray filteredArrayUsingPredicate: Predicate]];
                if (filtered.count>0)
                {
                    [test addObject:obj];
                }else
                {
                    [filtered addObjectsFromArray:[obj.EmailArray filteredArrayUsingPredicate: Predicate]];
                    if (filtered.count>0)
                    {
                        [test addObject:obj];
                    }
                }

            }

            DLog(@"%@",test);
            for (FreindsContact *obj in arraySearchResult)
            {
                if ([test containsObject:obj])
                {
                    [test removeObject:obj];
                }
            }
            if (test.count>0) {
                 arraySearchResult=test;
            }