尝试在字典中返回nil插入字典

时间:2016-09-29 17:39:28

标签: ios objective-c nsarray nsdictionary

我尝试将字典作为参数传递给方法,从而将字典插入到字典中。我有一个字典,其中键0,1,2,...,6由字典组成,我尝试在新字典中使用键0获取字典,但新字典为零。

dictionaryWithWeekDays由以下内容组成:

0 =     {
        clouds = 0;
        deg = 239;
        dt = 1475175600;
        humidity = 76;
        pressure = "993.71";
        speed = "3.24";
        temp =         {
            day = "70.09";
            eve = "74.73";
            max = "76.64";
            min = "51.53";
            morn = "54.66";
            night = "51.53";
        };
        weather =         (
                        {
                description = "light rain";
                icon = 10d;
                id = 500;
                main = Rain;
            }
        );
    };
    1 =     {
        clouds = 12;
        deg = 337;
        dt = 1475607600;
        humidity = 0;
        pressure = "1012.86";
        speed = "3.89";
        temp =         {
            day = "59.59";
            eve = "64.56";
            max = "64.56";
            min = "49.64";
            morn = "49.64";
            night = "54.32";
        };
        weather =         (
                        {
                description = "light rain";
                icon = 10d;
                id = 500;
                main = Rain;
            }
        );
    };

我有一个for循环,因为想要传递方法中的所有键。我只是将我的方法调用到这样的循环中:

for (int i = 0; i <= 6; i++) {
        NSString *key = [NSString stringWithFormat:@"%i", i];
        [self addValuesToArrayWithTemp:dictionaryWithWeekDays key:key index:i];
}

这是我的 addValuesToArrayWithTemp

-(void) addValuesToArrayWithTemp:(NSDictionary *)inputDic key:(NSString *)key index:(int)ind {
    NSDictionary *currentDic = [inputDic objectForKey:key];
    NSLog(@"currentDic = %@", currentDic);
    NSLog(@"key = %@", key);
   ...
   ...
}

(NSDictionary *)inputDic - 是的,它包含我之前传递的所有内容,关键也好吧@&#34; 0&#34;值。但是currentDic是零。为什么??也许我误解了一些事情。感谢帮助!

1 个答案:

答案 0 :(得分:2)

我假设您打印出的dictionaryWithWeekDays来自调试器。我相信你在dictionaryWithWeekDays中的键不是字符串,如果是,列表应该显示“0”=而不是0 =

您想要检查dictionaryWithWeekDays中键值的实际类型。