我们正在尝试创建一个从 API 获取 JSON 的函数。我们知道这给了我们 NIL,但我们不知道为什么会发生错误。我们得到的确切错误信息是
[] 2020-08-01 16:29:26.501199-0400 HEFT [97766:2952325] [] nw_proxy_resolver_create_parsed_array [C1 代理 pac] 评估错误:NSURLErrorDomain:-1003 无法将类型“NSNull”(0x7fff87a92380)的值转换为“NSString” ' (0x7fff87b502e8)。2020-08-01 16:29:26.670549-0400 HEFT [97766:2952139] 无法将“NSNull”(0x7fff87a92380)类型的值转换为“NSString”(0x7fff87b502e8)。(lldb)
我们试图弄乱代码以找到解决方案,并尝试使用其他一些问题,但它们都与我们试图实现的目标无关。
func getJson() {
if let url = URL(string: "https://api.weather.gov/alerts/active?area=GA") {
URLSession.shared.dataTask(with: url) { (data:Data?, response:URLResponse?, error:Error?) in
if error == nil {
if data != nil {
if let json = try? JSONSerialization.jsonObject(with: data!, options: []) as? [String:AnyObject] {
DispatchQueue.main.async {
//if let rawfeatures = json["features"] {
var rawfeatures = json["features"] as! [Dictionary< String, AnyObject>]
var keepgoingfeatures = rawfeatures.count
var FeatureIndex = 0
while keepgoingfeatures != 0{
let currentRawFeature = rawfeatures[FeatureIndex]
let currentRawFeatureProperties = currentRawFeature["properties"]
let currentFeature = Feature()
currentFeature.event = currentRawFeatureProperties!["event"] as! String
currentFeature.description = currentRawFeatureProperties!["description"] as! String
currentFeature.instructions = currentRawFeatureProperties!["instruction"] as! String
currentFeature.urgency = currentRawFeatureProperties!["urgency"] as! String
keepgoingfeatures -= 1
FeatureIndex += 1
}
}
}
}
} else {
print("We have an error")
}
}.resume()
}
}