我正在尝试CLLocation在Core Data中保存数据.我的属性设置为可转换对象.
一些示例代码表明可以使用此objc转换为a来保存位置数据NSValue.
CLLocationCoordinate2D myLocation;
NSValue *locationValue = [NSValue valueWithBytes:&myLocation objCType:@encode(CLLocationCoordinate2D)]
// then put locationValue into storage - maybe an ObjC collection class or core data etc.
Run Code Online (Sandbox Code Playgroud)
从数据存储中检索对象应该可以使用此范例.
CLLocationCoordinate2D myLocation;
NSValue *locationValue = // restored from your database or retrieved from your collection class
[locationValue getValue:&myLocation];
Run Code Online (Sandbox Code Playgroud)
我试图推断这个想法并将整个CLLocation对象捕获到NSValue存储中CoreData,目标CLLocation是稍后从商店中恢复对象.
在测试各种方法时,我发现在尝试CLLocation从Core Data Store中解包对象时返回了nil .我CLLocation使用以下内容将对象放入商店:
//verified that I have a good CLLocation here
newManagedObject.setValue(location as CLLocation, forKey: "location") …Run Code Online (Sandbox Code Playgroud)