Change
as! [[String:AnyObject]]
to
as? [[String:AnyObject]]
You are trying to force cast your json data (as!) so even though you are using guard you will still get a crash at that spot if its nil.
Edit: You said its still forcing as! so maybe try to split up your code like so. Should also make it more readable and easier for you to get other info out of the dicts/arrays of the json response. Something like this should work
/// Get json data
guard
let loadedWeather = json["weather"] as? [[String:AnyObject]],
let loadedTemperatur = json["main"] as? [String:AnyObject],
let loadedWindSpeed = json["wind"] as? [String:AnyObject]
else {
print("Weather JSON-Parsing failed")
return
}
/// Get info from json data
guard
let weatherDescription = loadedWeather[0]["description"] as? String,
let temperature = loadedTemperatur["temp"] as? Float,
let windSpeed = loadedWindSpeed["speed"] as? Float
else {
print("Weather JSON-Parsing failed")
return
}
/// do something with weather description, temperature, windSpeed
Maybe even better try to split up those guard statements for each line separately so incase one fails your whole block doesn't exit. In that case better to use if let because you dont want to exit early. Just dont start any pyramids of doom with if let statements.
/// Weather
if let loadedWeather = json["weather"] as? [[String:AnyObject]],
let weatherDescription = loadedWeather[0]["description"] as? String {
// do something with weather description
}
/// Temperature
if let loadedTemperatur = json["main"] as? [String:AnyObject],
let temperature = loadedTemperatur["temp"] as? Float {
// do something with temperature
}
/// Wind speed
if let loadedWindSpeed = json["wind"] as? [String:AnyObject],
let windSpeed = loadedWindSpeed["speed"] as? Float {
// do something with windspeed
}
Hope this helps.