我正在使用XCode7.2版本
我尝试使用locationManager委托函数
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last as! CLLocation
print("%f/%f",location.coordinate.latitude,location.coordinate.longitude)
}但我得到的错误如下:
Downcast from 'CLLocation?' to 'CLLocation' only unwraps optionals; did you mean to use '!'?我不知道怎么解决这个问题。
快速代码更改更多
有人能帮我解决吗?
谢谢
发布于 2015-12-29 13:24:17
locations.last返回类型是CLLocation?,这是因为数组可能根本没有元素。作为error状态,您不必将一个可选类型强制转换为非可选类型,只需这样做就可以打开它:
let location = locations.last!请注意,使用上述方法可能导致异常,并始终考虑使用可选的展开。
if let location = locations.last{
print("%f/%f",location.coordinate.latitude,location.coordinate.longitude)
}https://stackoverflow.com/questions/34511909
复制相似问题