【发布时间】:2016-02-05 07:36:31
【问题描述】:
使用 alamofire 调用时https://maps.googleapis.com/maps/api/directions/json?origin=(self.startlat,self.startlng)&destination=(self.endlat,self.endlng)
我一直收到这个错误:
'致命错误:在展开可选值时意外发现 nil'
我也试试这个
if let slat = startlat {
if let slng = self.startlng {
if let elat = self.endlat {
if let elng = self.endlng {
Alamofire.request(.GET, "https://maps.googleapis.com/maps/api/directions/json?origin=\(slat,slng)&destination=\(elat,elng)" , encoding: .JSON)
.responseJSON { response in
let res = response.result.value!
self.estimatedDistance = (res["routes"]!![0]["legs"]!![0]["distance"]!!["value"] as! Int)/1000
self.estimatedTime = (res["routes"]!![0]["legs"]!![0]["duration"]!!["value"] as! Int)/60
print("\(self.estimatedDistance) - \(self.estimatedTime)")
}
}
}
}
}
但我仍然遇到同样的错误。
【问题讨论】:
-
guard/else是你的朋友 ;) -
您仍然有几个强制展开。调试器光标指向哪一行?
-
保持强制展开从来都不是一个好主意,如果你对
if let或guard let感到懒惰,那么考虑使用Swifty JSON,它会有所帮助