2015-03-13 36 views
1

存儲MKMapItem,我是能夠通過查看編譯器警告解決時,我遇到了一個問題,但我不明白爲什麼能解決這個問題,或者如果我使用「最佳做法「。CLLocationCoordinates

我有一個對象模型,它將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

您的解決方案是完全合理和有效的。真的,沒有必要改變它或擔心你做錯了什麼,這是一個很好的解決方案。 – 2015-03-13 20:50:37

回答

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]; 

+0

非常感謝!我的「解決方案」感覺有點強迫/冒險。我很高興我發佈了一個問題,因爲我學到了一些東西。再次感謝。 – Adrian 2015-03-13 18:31:12

+1

@AdrianB很高興能幫到你。與你的項目玩得開心! – Steve 2015-03-13 18:32:52

+0

@Steve,你能澄清你爲什麼建議不*使用標量?相當一段時間以來,標量在託管對象的子類上得到了支持。作者的「解決方案」使用了標量,並且完全正確。 – quellish 2015-03-16 06:43:07