2013-10-30 19 views
0

在我的iOS應用程序中,我拉入JSON並將其設置爲數組。該陣列是這樣的:在數組級別上使用實例計數器的NSPredicate篩選器

links =   { 

      "link_1" =    { 

       clicks = 1; 

       ip = "10.0.0.0"; 

       shorturl = "http://on.example.com/abc"; 

       timestamp = "2013-10-30 05:56:51"; 

       title = Example Website; 

       url = "http://example.com/page/1/path/"; 

      }; 

      "link_2" =    { 

       clicks = 10; 

       ip = "10.0.0.0"; 

       shorturl = "http://on.example.com/def"; 

       timestamp = "2013-10-16 16:55:47"; 

       title = "Fitbit Force Is the Smartest Fitness Tracker Yet [REVIEW]"; 

       url = "http://mashable.com/2013/10/16/fitbit-force-review/"; 

      }; 

我有以下方法設置過濾UITableView結果:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{ 
    NSPredicate *resultPredicate = [NSPredicate 
            predicateWithFormat:@"links.link_1.title contains[cd] %@", 
            searchText]; 

    searchResults = [filterArray filteredArrayUsingPredicate:resultPredicate]; 

} 

我需要更新predicateWithFormat過濾器佔,link_2link_3...

問題:我如何讓過濾器匹配我的數組中的任何「級別」,其中字符串afte r link_始終是一個正數,但可以是無限數字的數字?

我基本上需要搜索link_級別的數組,每個正數。

回答

1

這不是一個數組,links是一本字典。因此只需使用格式爲@"title contains[cd] %@"的謂詞過濾links.allValues即可。

== ==編輯

如果我明白你的權利,你需要過濾的鏈接對象:

NSArray *linksArray = [[filterArray[0] objectForKey:@"links"] allValues]; 
NSPredicate *resultPredicate = [NSPredicate 
           predicateWithFormat:@"title contains[cd] %@", searchText]; 
NSArray *filteredLinks = [linksArray filteredArrayUsingPredicate:resultPredicate]; 

然後形成你需要它的形式searchResults,也許只是通過filteredLinks結果。

+0

'.allValues'從哪裏來?你可以給我一個例子使用我的代碼,所以我明白你的答案? - 謝謝! – adamdehaven

+0

@adamdehaven allValues是一個NSDictionary的方法。鏈接是一個NSDictionary對象,所以你應該得到所有的值並過濾它們。爲了給你舉個例子,我需要一些答案。代碼中的filterArray變量是什麼,它是如何得到的?它如何與打印的鏈接對象相關? –

+0

'links'對象(在我原來的問題中)實際上是'NSArray * filterArray'的'NSLog'。 'filterArray = [NSArray arrayWithObject:[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]];' – adamdehaven