在iOS中使用多个键计算字典

时间:2014-05-21 13:51:09

标签: ios count nsdictionary

这是我的JSON:

collections =     {
        items =         (
                        {
                nom = "automne-hiver-2012-2013";
            },
                        {
                nom = carbon;
            },
                        {
                nom = colors;
            });
    };
}

我可以像这样访问我的json:

int j=0;
NSString *collections=self.dictionaryMenu[@"collections"][@"items"][j][@"nom"];

我正在寻找所有关键的" nom"谁是我的JSON。 我试试:

for (objectKey in self.dictionaryMenu.allKeys) {
    count2 += [[self.dictionaryMenu objectForKey:objectKey] count];
               NSLog(@"Anzahl: %d", count2);
               }
               NSString *counter = [NSString stringWithFormat:@"COUNT MENU (%d)", count2];
               NSLog(@"%@",counter);

不幸的是,它发送给我1(我认为这是因为JSON中只有1"集合")。

我怎样才能计算所有关键" nom" ?

1 个答案:

答案 0 :(得分:0)

试试这个:

NSArray *items = self.dictionaryMenu[@"collections"][@"items"];
NSUInteger count = 0;
for (NSDictionary *item in items) {
    if (item[@"nom"]) {
        count++;
    }
}