kuemme01 kuemme01 - 2 months ago 27
Swift Question

Ambiguous use of 'subscript' after converting to swift 2.3

I got this Errors after I converting to 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
}


The
Ambiguous use of subscript
error comes by declaring "loadedWeather, loadedTemperatur and loadedWindSpeed".

Already tried to change NSDictionary to Dictionary and other things, helped on another position in code, but here....

thanks guys

Answer

I think that the issue is that the compiler cannot work out what json["weather"] is, You may need to be more specific in your code.

Try

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