2013-07-18 56 views
0

我有這樣使用NSPredicate與嵌套數組IOS

NSMutableArray *level1; 
NSMutableArray *level2; 
NSMutableArray *level3; 

self.questionSections=[[NSArray alloc] initWithObjects:@"Level 1",@"Level 2",@"Level 3", nil]; 

level1=[[NSMutableArray alloc] init]; 
level2=[[NSMutableArray alloc] init]; 
level3=[[NSMutableArray alloc] init]; 

[level1 addObject:[[NSDictionary alloc] initWithObjectsAndKeys: 
        @"What is the opposite of up?",@"name", 
        @"101q.png",@"questionpicture", 
        nil]]; 

[level1 addObject:[[NSDictionary alloc] initWithObjectsAndKeys: 
        @"What is the opposite of front?",@"name", 
        @"102q.png",@"questionpicture", 
        nil]]; 


[level2 addObject:[[NSDictionary alloc] initWithObjectsAndKeys: 
        @"Name a common pet?",@"name", 
        @"201q.png",@"questionpicture", 
        nil]]; 


[level2 addObject:[[NSDictionary alloc] initWithObjectsAndKeys: 
        @"Name a common bird?",@"name", 
        @"202q.png",@"questionpicture", 
        nil]]; 


[level3 addObject:[[NSDictionary alloc] initWithObjectsAndKeys: 
        @"List 3 rooms in your house?",@"name", 
        @"301q.png",@"questionpicture", 
        nil]]; 

self.questionData=[[NSArray alloc] initWithObjects:level1,level2,level3, nil]; 

一個數組,我想搜索。我想這一點,但還是一無所獲

NSPredicate *resultPredicate=[NSPredicate predicateWithFormat:@"name CONTAINS 'the'"]; 

NSLog(@"%@",resultPredicate); 
NSLog(@"%@",self.questionData); 

self.searchResults=[self.questionData filteredArrayUsingPredicate:resultPredicate]; 

NSLog(@"%@",self.searchResults); 

resultPredicate看起來很好 「名稱中包含 '的'」

self.questionData看起來不錯

self.searchResults僅僅是()

任何想法,以幫助將很棒。謝謝。

+0

http://stackoverflow.com/questions/1434291/can-an-nspredicate-search-for-an-object-within-an-array-owned-by-an-object在一個 – user523234

回答

0

這裏數組的問題。

(
     (
       { 
      name = "What is the opposite of up?"; 
      questionpicture = "101q.png"; 
     }, 
       { 
      name = "What is the opposite of front?"; 
      questionpicture = "102q.png"; 
     } 
    ), 
     (
       { 
      name = "Name a common pet?"; 
      questionpicture = "201q.png"; 
     }, 
       { 
      name = "Name a common bird?"; 
      questionpicture = "202q.png"; 
     } 
    ), 
     (
       { 
      name = "List 3 rooms in your house?"; 
      questionpicture = "301q.png"; 
     } 
    ) 
) 

的工作你上面謂語則數組必須是這樣的,

(
     { 
     name = "What is the opposite of up?"; 
     questionpicture = "101q.png"; 
    }, 
     { 
     name = "What is the opposite of front?"; 
     questionpicture = "102q.png"; 
    }, 
     { 
     name = "Name a common pet?"; 
     questionpicture = "201q.png"; 
    }, 
     { 
     name = "Name a common bird?"; 
     questionpicture = "202q.png"; 
    }, 
     { 
     name = "List 3 rooms in your house?"; 
     questionpicture = "301q.png"; 
    } 
) 

編輯: - 嘗試這樣,

NSArray *questionData=[[NSArray alloc] initWithObjects:level1,level2,level3, nil]; 

NSPredicate *resultPredicate=[NSPredicate predicateWithFormat:@"name CONTAINS 'the'"]; 


NSMutableArray *resultarray= [[NSMutableArray alloc]init]; 
for(int i=0;i<[questionData count];i++){ 
    NSArray* searchResults=[[questionData objectAtIndex:i] filteredArrayUsingPredicate:resultPredicate]; 
    if([searchResults count]>0) 
     [resultarray addObject:searchResults]; 
} 
NSLog(@"%@",resultarray); 
O/P:- 
(
     (
       { 
      name = "What is the opposite of up?"; 
      questionpicture = "101q.png"; 
     }, 
       { 
      name = "What is the opposite of front?"; 
      questionpicture = "102q.png"; 
     } 
    ) 
) 
+0

@ user2595265:一旦檢查編輯的答案。 – Balu

+0

太好了。謝謝。 – OzRoz

+0

使用子查詢可以解決問題,不需要使用for循環或更改數組結構。 –

5

我使用子查詢來解決這個

NSArray *questionData=[[NSArray alloc] initWithObjects:level1,level2,level3, nil]; 

NSPredicate *resultPredicate=[NSPredicate predicateWithFormat:@"SUBQUERY(name, $content, $content CONTAINS %@)[email protected] > 0", @"the"]; 

NSArray *searchResults=[questionData filteredArrayUsingPredicate:resultPredicate]; 

NSLog(@"%@",searchResults); 

結果::

(
    (
      { 
     name = "What is the opposite of up?"; 
     questionpicture = "101q.png"; 
    }, 
      { 
     name = "What is the opposite of front?"; 
     questionpicture = "102q.png"; 
    } 
) 

+0

+1對於很好的回答.... – Balu