繁体   English   中英

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 '!'?

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

快速代码更改更多

有谁可以帮助我解决问题?

谢谢

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

let location = locations.last!

请注意,使用上述方法可能会导致异常,请务必考虑使用可选的拆包

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM