[英]Storing CLLocationCoordinates in Core Data
我在存儲MKMapItem
時遇到了一個問題,該問題可以通過查看編譯器警告來解決,但是我不明白為什么我能夠解決它,或者我是否使用了“最佳實踐”。
我有一個對象模型,它將來自MKMapItem
緯度和經度坐標分別存儲為NSManagedObject
double
。 當我轉到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
解決它PointOfInterest : NSManagedObject
子類:
@property (nonatomic) double latitude;
@property (nonatomic) double longitude;
這是使編譯器滿意的最佳方法還是有更好的方法?
我建議您將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];
等等
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.