2014-07-01 86 views
0

我有我的plist另一個字典中的解釋如下:如何在nsdictionary中使用謂詞?

<dict1>Root 
     <dict2>A 
      <dict3>Apple 
        <string>"About Apple..." 
      <dict3>Arrow 
        <string>"About Arrow..." 
     <dict2>B 
      <dict3>Ball 
        <string>"About Ball..." 
     <dict2>C 

        etc... 

用戶正在使用搜索欄來搜索字符串。我如何看待第三級別,並使用謂詞比較每個字符串並返回結果並將它們傳遞給表視圖單元格?

我試過下面這個視頻教程,但是我在謂詞上失敗了。

https://www.youtube.com/watch?v=UE4h_Td6W6U

他寫他的字典到一個數組從我所看到的。我的看起來有點複雜。

我是新來的,所以任何幫助將不勝感激。

回答

2

您需要遍歷字典列表,直到找到您正在查找的字典。它看起來是這樣的:

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"YOURPLIST" ofType:@"plist"]; 
    NSMutableDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath]; //This puts you into the root directory 
    NSMutableDictionary *secondLevel = [NSMutableDictionary dictionaryWithDictionary:[dictionary objectForKey:[NSString stringWithFormat:@"A"]]]; //or B or C etc 
    NSMutableDictionary *thirdLevel = [NSMutableDictionary dictionaryWithDictionary:[dictionary objectForKey:[NSString stringWithFormat:@"Apple"]]]; 

這將把你放在蘋果字典,但是能夠獲取字符串,你需要一個附加到它的密鑰。你的字典應該像

<key>Some key name</key> 
    <string>About Apple...</string> 

然後你就可以獲取使用該密鑰通過串:

NSString *string = [thirdLevel objectForKey:@"Some key name"]; 

如果你嚴格搜索字符串,你可以做這樣的事情Search String in NSDictionary store in NSMutableArray