在NSManagedObject上设置double属性的值

dme*_*s89 4 iphone core-data objective-c ios

我正在尝试在我的应用程序上实现一个地图功能.但是我想将纬度和经度从存储在核心数据中的对象传递到地图中.但是,当我启动应用程序时,我在设置对象的初始值时遇到问题.到目前为止,我已经尝试了两种不同的方式,我收到每错误"发送"双到不兼容类型的参数'身份证’.任何帮助将是非常赞赏.

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"];
Run Code Online (Sandbox Code Playgroud)

yuj*_*uji 12

NSManagedObject属性需要是对象,但是double是原始的C类型.解决方案是将双打包装在NSNumber:

[room setValue:[NSNumber numberWithDouble:myLongitude] forKey:@"longitude"];
[room setValue:[NSNumber numberWithDouble:myLatitude] forKey:@"latitude"];
Run Code Online (Sandbox Code Playgroud)