Search code examples
objective-cioscllocation

Objective C: Error when trying to initialize instance of CLLocation


I am hitting an error (program crashed) when it hits the following code

CLLocation *userLoc = [[CLLocation alloc] initWithCoordinate:locationManager.location.coordinate];

I tried to replace the argument with the following and I get the same error as well

CLLocation *userLoc = [[CLLocation alloc] initWithCoordinate:mapView.userLocation.coordinate];

Can anyone advise me on what should be the correct argument to use here for the user's current location?

Thanks!

Zhen

Stacktrace:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CLLocation initWithCoordinate:]: unrecognized selector sent to instance 0x1e0620'

Solution

  • According to the documentation, CLLocation does not have an initializer called initWithCoordinate:. Use one of the following:

    • initWithLatitude:longitude:
    • initWithCoordinate:altitude:horizontalAccuracy:verticalAccuracy:timestamp:
    • initWithCoordinate:altitude:horizontalAccuracy:verticalAccuracy:course:speed:timestamp: