2015-12-12 112 views
-1

我有一个搜索嵌套阵列multple键+ NSpredicate

(
    { 
    ContactID = 100; 
    Emails =  (

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

- 我要搜索使用NSPredicate的PHONENUMBERS全名,PHONENUMBERS并从上面数组电子邮件和电子邮件包含数组,所以我有搜索来自嵌套数组的值,给我建议如何使用这种格式搜索值。

回答

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; 
      }