Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 644
pilou Avatar asked Apr 06 '18 08:04

pilou


1 Answers

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:

  • https://developer.apple.com/documentation/corelocation/cllocationmanagerdelegate/1423716-locationmanager
  • https://developer.apple.com/documentation/corelocation/cllocationmanagerdelegate/1423615-locationmanager
like image 69
staticVoidMan Avatar answered Nov 12 '22 23:11

staticVoidMan