尝试从数组中获取字符串(打开天气图)
Trying to get a String out of an Array (Open Weather Map)
我正在尝试使用 swift 从 Open Weather Map API (json) 获取天气数据。我设法使用这个
访问了温度
if let main = json["main"] as? NSDictionary {
println(main)
if var temp = main["temp"] as? Double {
temperatureLabel.text = String(format: "%.1fº K", temp)
}
}
很遗憾,我无法访问天气说明!我试过了
if let weather = json["weather"] as? NSArray {
println(weather)
if var temp = weather["description"] as? String {
descriptionLabel.text = weatherDescription
}
}
我只是不知道该尝试什么,因为我是 Swift 的新手,之前从未使用过 json。
weather
是字典的 array
所以 array
需要下标如下:
if let weather = json["weather"] as? NSArray {
println(weather)
if var temp = weather[0]["description"] as? String {
descriptionLabel.text = weatherDescription
}
}
我正在尝试使用 swift 从 Open Weather Map API (json) 获取天气数据。我设法使用这个
访问了温度if let main = json["main"] as? NSDictionary {
println(main)
if var temp = main["temp"] as? Double {
temperatureLabel.text = String(format: "%.1fº K", temp)
}
}
很遗憾,我无法访问天气说明!我试过了
if let weather = json["weather"] as? NSArray {
println(weather)
if var temp = weather["description"] as? String {
descriptionLabel.text = weatherDescription
}
}
我只是不知道该尝试什么,因为我是 Swift 的新手,之前从未使用过 json。
weather
是字典的 array
所以 array
需要下标如下:
if let weather = json["weather"] as? NSArray {
println(weather)
if var temp = weather[0]["description"] as? String {
descriptionLabel.text = weatherDescription
}
}