Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the value of a double attribute on NSManagedObject

I am trying to implement a maps feature on my application. However to I would like the latitude and longitude be passed into the map from an object that is stored in core data. However I am having problem setting the initial value on the object when I start my application. I have tried 2 different ways so far and each and error I receive is "Sending 'double to a parameter of incompatible type 'id'. Any help would be very appreciated.

NSManagedObject *room = [NSEntityDescription insertNewObjectForEntityForName:@"Room" inManagedObjectContext:context];

    double myLatitude = -1.228087;
    double myLongitude = 52.764397;

    [room setValue:@"H001" forKey:@"code"];
    [room setValue:@"This is a description" forKey:@"roomDescription"];
    [room setValue:myLatitude forKey:@"longitude"];
    [room setValue:myLatitude forKey:@"latitude"];
like image 877
dmeads89 Avatar asked Dec 09 '22 01:12

dmeads89


2 Answers

NSManagedObject attributes need to be objects, but double is a primitive C type. The solution is to wrap the doubles in an NSNumber:

[room setValue:[NSNumber numberWithDouble:myLongitude] forKey:@"longitude"];
[room setValue:[NSNumber numberWithDouble:myLatitude] forKey:@"latitude"];
like image 157
yuji Avatar answered Mar 15 '23 04:03

yuji


You should wrap your double as an NSNumber:

[NSNumber numberWithDouble:myDouble]

NSManagedObject setValue:forKey: requires an id for it's value. The double is a primitive.

like image 45
wmorrison365 Avatar answered Mar 15 '23 04:03

wmorrison365