这是出现错误的功能。
func LoadMapRoute(Url:String)
{
let url = URL(string: Url)
let session = URLSession(configuration: .default)
let task = session.dataTask(with: url!, completionHandler: {
(data, response, error) in
guard error == nil else {
print(error!.localizedDescription)
return
}
guard let jsonResult = try? (JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any]),
let jsonResponse = jsonResult else {
print("error in JSONSerialization")
return
}
//Call this method to draw path on map
self.drawPath(from: polyLineString)
})
task.resume()
}
这是在guard let = jsonResult中显示错误的行
let jsonResponse = jsonResult
最佳答案
请引用以下代码:
guard let jsonResult = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments),let jsonResponse = jsonResult as? [String: Any] else {
print("error in JSONSerialization")
return
}
您的
jsonResult
已经解包,因此无需再次解包。谢谢
关于ios - “Initializer for conditional binding must have Optional type, not ' [String : Any] '” error in swift,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59372166/