转换为swift后模糊使用“下标”2.3

转换到swift 2.3后,我得到了这个错误。

guard let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary else { throw JSONError.ConversionFailed } guard let loadedWeather = json["weather"]![0]["description"] as? String, let loadedTemperatur = json["main"]!["temp"] as? Float, let loadedWindSpeed = json["wind"]!["speed"] as? Float else { print("Weather JSON-Parsing failed") return } 

Ambiguous use of subscript错误的Ambiguous use of subscript来自声明“loadedWeather,loadedTemperatur和loadedWindSpeed”。

已经尝试将NSDictionary更改为字典和其他的东西,帮助在代码中的另一个位置,但在这里….

多谢你们

我认为问题在于编译器无法计算出json["weather"]是什么,您可能需要在代码中更加具体。

尝试

 let loadedWeather = (json["weather"] as! [[String:AnyObject]])[0]["description"] as? String 

发生这种情况是因为编译器不知道中间对象在每行中是什么…所以可能是这样

  if let weather = json["weather"] as? [[String:String]], firstObject = weather.first as? [String:String]{ let loadedWeather = firstObject["description"] } // same for other objects ie `json["main"]` and `json["wind"]` with its return type