2016-11-27 62 views
0

我正在從天氣API獲取數據。我不確定如何訪問說明?Swift 3:遍歷_NSSingleObjectArrayI

"weather": <__NSSingleObjectArrayI 0x608000012910>(
{ 
    description = "overcast clouds"; 
    icon = 04n; 
    id = 804; 
    main = Clouds; 
} 
) 

我想:

print(weatherDict["weather"]!.description!) 

它只是給了我這個:

(
    { 
    description = "overcast clouds"; 
    icon = 04n; 
    id = 804; 
    main = Clouds; 
    } 
) 

如何正確訪問說明?

回答

4
  • weather包含字典數組。
  • description是數組中第一項的關鍵。

代碼解開weather安全檢查該數組不爲空:

if let weather = weatherDict["weather"] as? [[String:Any]], !weather.isEmpty { 
    print(weather[0]["description"]) // the value is an optional. 
}