Search code examples
iosnsnumberincompatibletypeerror

Assigning to NSNumber from incompatible type double


In a header file, I am declaring the following property:

//lat
@property (nonatomic, assign) NSNumber *lat;

Then in the implementation file I get a double variable from an array:

double lat1 = [[[categorias objectAtIndex:i]objectForKey:@"latitud"] doubleValue];

and then I want to assign this value to the previous declared lat instance:

annotation3.lat = lat1;

But doing this throws an error:

Assigning to NSNumber from incompatible type double.

What should I change to do this without errors?


Solution

  • Use NSNumber literal syntax:

    annotation3.lat = @( lat1 );
    

    or create an instance the old fashioned way:

    annotation3.lat = [NSNumber numberWithDouble:lat1];
    

    Also, be sure you just want your property to use assign (you usually want to use strong).