【问题标题】:Parsing a Dictionary of Arrays from a Plist file从 Plist 文件中解析数组字典
【发布时间】:2015-06-10 19:38:35
【问题描述】:

我正在尝试使用特定的数据布局。我们使用一个 plist 文件,它通过以下方式设置: 根(字典):
- 1.1(阵列)
-- 第 0 项(字典)
--- 标题
--- 图标
-- 第 1 项(字典)
--- 标题
--- 图标
- 1.2(阵列)
-- 第 0 项(字典)
--- 标题
--- 图标

来自 plist 的这些信息被解析成一个字典,该字典如下所示:

{
    "1.0" =     (
                {
            detail = "Detail 0";
            icon = "Pull to Refresh";
            title = "Item 0";
        },
                {
            detail = "More easily refresh a subscription or playlist.";
            icon = "Pull to Refresh";
            title = "Pull to Refresh";
        }
    );
    "1.1" =     (
                {
            detail = "Create custom stations of your favorite podcasts.";
            icon = Stations;
            title = "Custom Stations";
        }
    );
}

我想要找出的是子数组中的项目数。例如,有两个子数组,一个用于 1.1,一个用于 1.2。我希望从这些数组中获得字典的数量?

有没有快速访问它的方法。我试图完成一个 for 循环并计算它们,但我收到 NSCFString 错误,好像我的数据不正确?

【问题讨论】:

    标签: ios objective-c nsarray nsdictionary


    【解决方案1】:

    既然你已经提到自己尝试一个 for 循环,我不知道这是否真的是你正在寻找的那种答案,但出于这个非常特定的目的(即你确定输入字典总是有那种您描述的结构)沿着这些思路的东西应该可以解决问题:

    - (NSUInteger)numberOfItemsInSubArraysOfDictionary:(NSDictionary *)dict
    {
        NSUInteger count = 0;
        for(NSArray *array in dict.allValues) {
            if ([array isKindOfClass:[NSArray class]]) {
                count += array.count;
            }
        }
        return count;
    }
    

    【讨论】:

    • 啊,看到 Artal 的回答,我意识到我无法从您的问题中真正看出您是否要确定 字典中包含的子数组中的项目总数(正如我最初理解的那样)或每个子数组的项目数
    • 你是正确的。谢谢
    【解决方案2】:

    你可以试试这样的:

    for (NSString *key in [dictionary allKeys]) {
        NSLog(@"Number of dictionaries for %@: %d", key, [dictionary[key] count]);
    }
    

    这个循环的输出是:

    Number of dictionaries for 1.0: 2
    Number of dictionaries for 1.1: 1
    

    【讨论】:

      猜你喜欢
      • 2012-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-02
      • 1970-01-01
      • 2018-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多