Swift条件向下广播不起任何作用
在上面的代码中,我的理解是因为locations.last可能为零,所以我希望仔细地将其展开。我认为上面的代码对我来说是有意义的,但是有一个警告“从CLLocation到CLLocation的条件向下转换不起任何作用”。怎么了?您不需要将上一个位置对象强制转换为Swift条件向下广播不起任何作用,swift,Swift,在上面的代码中,我的理解是因为locations.last可能为零,所以我希望仔细地将其展开。我认为上面的代码对我来说是有意义的,但是有一个警告“从CLLocation到CLLocation的条件向下转换不起任何作用”。怎么了?您不需要将上一个位置对象强制转换为CLLocation,因为它已经是CLLocation 因此,您可以检查您的最后位置是否为零 func locationManager(_ manager: CLLocationManager, didUpdateLocations l
CLLocation
,因为它已经是CLLocation
因此,您可以检查您的最后位置是否为零
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
if let location = locations.last as? CLLocation {
//got warning Conditional downcast from CLLocation? to CLLocation does nothing
}
}
最后一个
已经是可选的计算属性。
如果您想确保这不是零,只需通过If let
或guard let
构造使用安全展开即可
有关上次的信息
->
我还写了详细的文章和例子
e、 g
或
if let lastLocation = locations.last {
// you can use lastLocation
}
if let lastLocation = locations.last {
print(lastLocation)
// TODO: - work with lastLocation instance
}
guard let lastLocation = locations.last else {
return
}
print(lastLocation)
// TODO: - work with lastLocation instance