在核心数据中存储CLLocation坐标

时间:2015-03-13 17:40:16

标签: ios core-data mapkit cllocation mkmapitem

我通过查看编译器警告来存储MKMapItem时遇到了一个问题,但是我不明白为什么我能够解决它,或者我是否使用了最佳实践"。

我有一个对象模型,它将MKMapItem中的纬度和经度坐标分别存储为double中的NSManagedObject。当我转到Editor\Create NSManagedObject Subclass并创建我的班级时,标题如下所示:

@class LocationCategory;

@interface PointOfInterest : NSManagedObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * address;
// Xcode spat out NSNumber instead of the double specified in the model setup screen
@property (nonatomic, retain) NSNumber * latitude;
@property (nonatomic, retain) NSNumber * longitude;
@property (nonatomic, retain) NSString * note;
@property (nonatomic, retain) LocationCategory *locationCategory;

@end

一切顺利,直到我尝试向我的managedObjectContext添加一个对象我收到了这些警告:

Assigning to 'NSNumber *' from incompatible type 'CLLocationDegrees' (aka 'double')

在这些方面:

newPOI.latitude = self.item.placemark.location.coordinate.latitude;
newPOI.longitude = self.item.placemark.location.coordinate.longitude;

我通过更改PointOfInterest : NSManagedObject子类来修复它:

@property (nonatomic) double latitude;
@property (nonatomic) double longitude;

这是让编译器开心还是有更好方法的最好方法?

1 个答案:

答案 0 :(得分:1)

我建议您将PointOfInterest子类的属性更改回NSNumber,然后按如下方式更改纬度和经度分配:

newPOI.latitude = [NSNumber numberWithDouble:self.item.placemark.location.coordinate.latitude];
newPOI.longitude = [NSNumber numberWithDouble:self.item.placemark.location.coordinate.longitude];

然后当你想使用纬度时:

self.item.placemark.location.coordinate.latitude = [newPOI.latitude doubleValue];