Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift条件向下广播不起任何作用_Swift - Fatal编程技术网

Swift条件向下广播不起任何作用

Swift条件向下广播不起任何作用,swift,Swift,在上面的代码中,我的理解是因为locations.last可能为零,所以我希望仔细地将其展开。我认为上面的代码对我来说是有意义的,但是有一个警告“从CLLocation到CLLocation的条件向下转换不起任何作用”。怎么了?您不需要将上一个位置对象强制转换为CLLocation,因为它已经是CLLocation 因此,您可以检查您的最后位置是否为零 func locationManager(_ manager: CLLocationManager, didUpdateLocations l

在上面的代码中,我的理解是因为locations.last可能为零,所以我希望仔细地将其展开。我认为上面的代码对我来说是有意义的,但是有一个警告“从CLLocation到CLLocation的条件向下转换不起任何作用”。怎么了?

您不需要将上一个位置对象强制转换为
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