简体   繁体   中英

ios CLLocation didUpdateLocations error

I am use XCode7.2 Version

I try to use the locationManager delegate function

 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? 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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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