实现不推荐使用的方法-CLLocation

使用Xcode 9.3,我有了新的警告。

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

现在不推荐使用此方法。“实现不推荐使用的方法”。我有解决办法吗?谢谢

staticVoidMan

对苹果的文档locationManager(_:didUpdateTo:from:)会告诉你使用locationManager(_:didUpdateLocations:)


因此,对于新的委托locationManager(_:didUpdateLocations:)locations对象的文档说明:

地点

包含位置数据的CLLocation对象数组。该数组始终包含至少一个表示当前位置的对象。如果更新被推迟,或者在交付之前到达多个位置,则阵列可能包含其他条目。数组中的对象按照它们发生的顺序进行组织。因此,最近的位置更新在阵列的末尾。

基本上,这意味着数组中至少有1个位置,如果多于1个,则:

  1. locations数组中的最后一个对象将是新位置/当前位置
  2. locations数组中的倒数第二个对象将是旧位置

范例(Swift 4+):

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let newLocation = locations.last

    let oldLocation: CLLocation?
    if locations.count > 1 {
        oldLocation = locations[locations.count - 2]
    }
    //...
}

示例(目标-C):

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    CLLocation *newLocation = locations.lastObject;

    CLLocation *oldLocation;
    if (locations.count > 1) {
        oldLocation = locations[locations.count - 2];
    }
    //...
}

参考:

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章