简体   繁体   English

ios CLLocation didUpdateLocations错误

[英]ios CLLocation didUpdateLocations error

I am use XCode7.2 Version 我正在使用XCode7.2版本

I try to use the locationManager delegate function 我尝试使用locationManager委托函数

 func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
 let location = locations.last as! CLLocation
 print("%f/%f",location.coordinate.latitude,location.coordinate.longitude)

 }

But I get the error below: 但是我得到以下错误:

  Downcast from 'CLLocation?' to 'CLLocation' only unwraps optionals; did you mean to use '!'?

I don't know how to fix the problem . 我不知道如何解决这个问题。

The swift code change more 快速代码更改更多

have anyone can help me how to fix? 有谁可以帮助我解决问题?

thank you 谢谢

locations.last return type is CLLocation? locations.last返回类型是CLLocation? and thats because an array may have no elements at all. 那就是因为数组可能根本没有任何元素。 As error state you don't have to cast an optional type to non optional when you can just unwrap it by doing this: 作为错误状态,当您只需通过以下操作将其解包时,就不必将可选类型强制转换为非可选类型:

let location = locations.last!

Be aware that using the above method can lead to exception and always consider using optional unwrapping 请注意,使用上述方法可能会导致异常,请务必考虑使用可选的拆包

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