func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
if let location = locations.last as? CLLocation {
//got warning Conditional downcast from CLLocation? to CLLocation does nothing
}
}
In the code above, my understanding is that since locations.last could be nil, so i want to unwrap it carefully. I thought the above code make sense to me but there is a warning "Conditional downcast from CLLocation? to CLLocation does nothing". What is wrong?
The last is already an optional computed property.
If you want be sure that isn't nil just use safe unwrap via if let or guard let construction.
Information about last -> Developer Apple
Also I write detailed article with examples My Medium Article
e.g.
if let lastLocation = locations.last {
print(lastLocation)
// TODO: - work with lastLocation instance
}
or
guard let lastLocation = locations.last else {
return
}
print(lastLocation)
// TODO: - work with lastLocation instance
You do not need to cast your last location object to CLLocation as it is already CLLocation.
So you can check whether your last location is nil or not like this.
if let lastLocation = locations.last {
// you can use lastLocation
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With