pilou
pilou

Reputation: 75

Implementing deprecated method - CLLocation

With Xcode 9.3, I've a new warning.

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

this method is now deprecated."Implementing deprecated method". I've you got a solution ? thanks

Upvotes: 5

Views: 2711

Answers (1)

staticVoidMan
staticVoidMan

Reputation: 20244

The Apple Documentation on locationManager(_:didUpdateTo:from:) will tell you to use locationManager(_:didUpdateLocations:)


So for the new delegate locationManager(_:didUpdateLocations:), the documentation on the locations object states:

locations

An array of CLLocation objects containing the location data. This array always contains at least one object representing the current location. If updates were deferred or if multiple locations arrived before they could be delivered, the array may contain additional entries. The objects in the array are organized in the order in which they occurred. Therefore, the most recent location update is at the end of the array.

Basically it means that there will be atleast 1 location in the array and if there are more than 1 then:

  1. The last object in locations array will be the new/current location
  2. The second last object in locations array will be the old location

Example (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]
    }
    //...
}

Example (Objective-C):

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

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

Ref:

Upvotes: 5

Related Questions