So habe ich einen Fehler für eine thread 4: SIGABRT
auf Xcode beim Versuch, Daten von der OpenWeatherApp API zu analysieren. Der Fehler, der auf der Konsole zieht ist:Fehler beim Versuch, Daten von einem JSON zu erhalten. Swift
nicht Wert vom Typ werfen kann ‚__NSArrayM‘ (0x3419714) auf ‚NSDictionary‘ (0x3419958)
ich verschiedene Dinge auf diesem Forum sah bereits und nichts scheint wirklich zu funktionieren.
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.
}}
Es scheint ein Fehler mit der folgenden Zeile zu sein:
let description = jsonResult["weather"]["description"]as! [String: Any]
Danke im Voraus für alle Hilfe!