2015-12-12 118 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; 
      }