ios CLLocation didUpdateLocations 错误

ios CLLocation didUpdateLocations error

我用的是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 '!'?

我不知道如何解决这个问题。

swift代码改动更多

有谁能帮我解决一下吗?

谢谢

locations.last return 类型是 CLLocation? 那是因为数组可能根本没有元素。作为错误状态,您不必将可选类型强制转换为非可选类型,您只需通过以下方式将其解包即可:

let location = locations.last!

请注意,使用上述方法可能会导致异常,请始终考虑使用可选的解包

if let location = locations.last{
    print("%f/%f",location.coordinate.latitude,location.coordinate.longitude)
}