-2

So I've been getting an error for a thread 4: SIGABRT on Xcode when trying to parse data from the OpenWeatherApp API. The error that pulls up on the console is:

Could not cast value of type '__NSArrayM' (0x3419714) to 'NSDictionary' (0x3419958)

I looked at different things on this forum already and none of it seems to really work.

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var typeCity: UITextField!




var weatherDescription : String = ""
override func viewDidLoad() {
    super.viewDidLoad()

  //  let checkText = typeCity.text
   /*
    if typeCity?.text == nil{
        typeCity.placeholder = "Type a city with no spaces"
        let city = "Chicago"
    }
   */
   let city = "Chicago"

    /*
    if checkText != nil {
        typeCity?.text = city
    }
    */
          print("City: \(city)")

    // Do any additional setup after loading the view, typically from 
 a nib.
    let url = URL(string: 
 "http://api.openweathermap.org/data/2.5/weather?q=\
 (city)&appid=626a124ef0844d2e021329c38a5dfafd")
        let task = URLSession.shared.dataTask(with: url!) { (data, 
response, error) in
        if error != nil{
            print(error!)
        } else {
            if let urlContent = data {
                do {
                    let jsonResult = try 
 JSONSerialization.jsonObject(with: urlContent, options: 
JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
                    print(jsonResult)
                    //let lon = jsonResult["coord"]["lon"].double
                    //let lat  = jsonResult["coord"]["lon"].double
                    //let temp = jsonResult?["main"]["double"].double
                    //print


                    print(jsonResult["name"]!!)


                    let coordinates = jsonResult["coord"] as! [String:Any]//the coordinates parsing
                    print("Coordinates: \(coordinates)")
                    let lon = coordinates["lon"] as! Double
                    let lat = coordinates["lat"] as! Double
                    print("Latitude: \(lat) Longitude: \(lon)")

                    let main = jsonResult["main"] as! 
[String:Any]//for the temperature

                    let kelvin = main["temp"] as! Double
                    let degreesFahrenheit = 9/5 * (kelvin-273) + 32
                    print("Temperature: \(degreesFahrenheit)")
                    let humidity = main["humidity"] as! Double
                    let pressure = main["pressure"] as! Double
                    let temp_max = main["temp_max"] as! Double
                    let temp_min = main["temp_min"] as! Double

                    let description = jsonResult["weather"]
 ["description"]as! [String: Any]

                    print("description")


                } catch{
                    print("Json Processing has failed or city name not recognized.")
                }
               // let json = jsonResult(data: data)


                //print("Lat: \(String(describing: lat)) Lon: \
 (String(describing: lon)) Temp: \(String(describing: temp))")




            }
        }
    }
    task.resume()
}

    override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}}

There seems to be an error with the following line:

let description = jsonResult["weather"]["description"]as! [String: Any]

Thank you in advance for all the help!

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223

1 Answers1

0

You are trying to implicitly convert an array to a dictionary. Try to safely check the type of it:

if let description = jsonResult["weather"]["description"] as? [String] {
    [...]
}
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223