Search code examples
ioscllocation

Sending 'NSNumber *__strong' to parameter of incompatible type 'CLLocationDegrees' (aka 'double')


NSNumber * latitude =  [NSNumber numberWithDouble:[[cityDictionary valueForKeyPath:@"coordinates.latitude"]doubleValue]];

      NSNumber * longitude =  [NSNumber numberWithDouble:[[cityDictionary valueForKeyPath:@"coordinates.longitude"]doubleValue]];


    CLLocation *listingLocation = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];

I am getting the following error on line # 3 above:

Sending 'NSNumber *__strong' to parameter of incompatible type 'CLLocationDegrees' (aka 'double')

I know it is because I am trying to pass a NSNumber to a place where it expects a double. But casting is not working due to ARC?


Solution

  • The call to [cityDictionary valueForKeyPath:@"coordinates.latitude"] is already giving you an NSNumber object. Why convert that to a double and then create a new NSNumber?

    You could just do:

    NSNumber *latitude = [cityDictionary valueForKeyPath:@"coordinates.latitude"];
    NSNumber *longitude = [cityDictionary valueForKeyPath:@"coordinates.longitude"];
    CLLocation *listingLocation = [[CLLocation alloc] initWithLatitude:[latitude doubleValue] longitude:[longitude doubleValue]];
    

    If it turns out that [cityDictionary valueForKeyPath:@"coordinates.latitude"] is actually returning an NSString and not an NSNumber, then do this:

    CLLocationDegrees latitude = [[cityDictionary valueForKeyPath:@"coordinates.latitude"] doubleValue];
    CLLocationDegrees longitude = [[cityDictionary valueForKeyPath:@"coordinates.longitude"] doubleValue];
    CLLocation *listingLocation = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];