gpt4 book ai didi

ios - 在核心数据中存储 CLLocation 坐标

转载 作者:行者123 更新时间:2023-11-29 12:26:08 24 4
gpt4 key购买 nike

我在存储 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 子类来修复它:

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

等等

关于ios - 在核心数据中存储 CLLocation 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29038631/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com