This is the JSON:
{
"main":{
"temp":-11.67,
"pressure":1040.21,
"humidity":73,
"temp_min":-11.67,
"temp_max":-11.67,
"sea_level":1055.37,
"grnd_level":1040.21
},
"id":3099213,
"name":"London",
"cod":200
}
I've got JSON file and I want to take the value of "name": "London" (London)
and save it in the variable cityName, but it doesn't work there is error in line self.cityName = city
. Cannot assign value of type [String : AnyObject]
to type String!
For example next one that have to save to variable degree temperature from JSON work correct and show temperature.
I don't know how to pick this "name":"London"
value and save it to variable.
When I modify and put this: self.cityName = city as! String
I've got warning Cast from '[String : AnyObject]'
to unrelated type 'String'
always fails:
var cityName: String!
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject]
if let main = json["main"] as? [String : AnyObject] {
if let temp = main["temp"] as? Int {
self.degree = temp
}
}
if let city = json["name"] as? [String : AnyObject] {
self.cityName = city
}
}
if let city = json["name"] as? [String : AnyObject] {
self.cityName = city
}
Replace to
if let city = json["name"] as? String {
self.cityName = city
}