【问题标题】:How to use predicate with nsdictionary?如何在 nsdictionary 中使用谓词?
【发布时间】:2014-07-01 00:04:20
【问题描述】:

我的 plist 中的另一个字典中有一个字典,如下所示:

    <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

他已将他的字典写入我所看到的数组中。我的好像有点复杂。

我是新手,因此我们将不胜感激。

【问题讨论】:

    标签: objective-c plist uisearchbar predicate


    【解决方案1】:

    您需要遍历您的字典列表,直到找到您要查找的字典。它看起来像这样:

        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"]]]; 
    

    这会将您放入 Apple 字典中,但是要能够获取字符串,您需要附加一个键。你的字典应该看起来像

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

    然后您可以通过以下方式使用密钥获取字符串:

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

    如果您严格搜索字符串,您可以执行类似Search String in NSDictionary store in NSMutableArray 的操作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-04
      • 1970-01-01
      • 2014-08-02
      • 1970-01-01
      • 1970-01-01
      • 2018-05-11
      相关资源
      最近更新 更多