设置NSManagedObject上的double属性的值 [英] Setting the value of a double attribute on NSManagedObject
问题描述
我试图在我的应用程序上实现地图功能。然而,我想要的纬度和经度从存储在核心数据中的对象传递到地图。但是,当我启动我的应用程序时,我有问题设置对象的初始值。我尝试了两种不同的方式到目前为止,每个和错误我收到的是发送双参数的不兼容的类型'id'。任何帮助将非常感激。
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"];
推荐答案
NSManagedObject
属性需要是对象,但 double
是一个原始的C类型。解决方案是在
NSNumber
中封装双精度:
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"];
这篇关于设置NSManagedObject上的double属性的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!