2016-09-29 68 views
0

我嘗試通過將字典作爲參數傳入方法來將字典插入到字典中。我有一個包含字典的鍵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 (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 - 是確定它包括所有我面前經過,關鍵還好吧有@ 「0」值。但是currentDic是零。爲什麼??也許我誤解了一些東西。感謝幫助!

+0

在調用'addValues之前,你能否一次檢查''key'的'dictionaryWithWeekDays'是否有一些值,並在這裏發表你的發現。 – prabodhprakash

+0

你可以顯示代碼如何生成此字典 –

+1

也試試這一次'[inputDic objectForKey:@ 0];' –

回答

2

我假設你打印的dictionaryWithWeekDays來自調試器。我相信你在dictionaryWithWeekDays中的密鑰不是字符串,如果它們是,列表應該顯示「0」=而不是0 =

你想檢查dictionaryWithWeekDays中的鍵值的實際類型。

+0

很好的觀察。 – prabodhprakash

+0

@OwenHartnett謝謝你的回答! –

+1

*如果他們是,上市應該顯示「0」*不正確,你注意到他的字典中的其他鍵?當在控制檯上打印時,如果鍵包含空格或_將顯示在「」中,否則「」將不會顯示。在這種情況下,您的答案可能是正確的,但引用的文本不是。 –