我想将位置坐标保存到核心数据中 - 存储这些并检索它们的正确方法是什么?
目前我正在这样做并遇到错误:
var newPlaceCoordinate = CLLocationCoordinate2D()
var newPlaceLatitude = CLLocationDegrees()
var newPlaceLongitude = CLLocationDegrees()
我从使用自动完成小部件的Google Maps API中获得了我的坐标。
let newPlaceCoordinate = place.coordinate
self.newPlaceCoordinate = place.coordinate
let newPlaceLatitude = place.coordinate.latitude
print(newPlaceLatitude)
self.newPlaceLatitude = place.coordinate.latitude
let newPlaceLongitude = place.coordinate.longitude
print(newPlaceLongitude)
self.newPlaceLongitude = place.coordinate.longitude
然后存储我使用的坐标:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let newPlace = NSEntityDescription.insertNewObject(forEntityName: "StoredPlace", into: context)
newPlace.setValue(newPlaceCoordinate, forKey: "coordinate")
newPlace.setValue(newPlaceLatitude, forKeyPath: "latitude")
newPlace.setValue(newPlaceLongitude, forKeyPath: "longitude")
我将所有属性设置为字符串类型。然后在我的ViewDidload中检索:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "StoredPlace")
request.returnsObjectsAsFaults = false
if let coordinate = result.value(forKey: "coordinate") as? String
{
let latitude = (coordinate as NSString).doubleValue
let longitude = (coordinate as NSString).doubleValue
let markers = GMSMarker()
markers.position = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
markers.icon = GMSMarker.markerImage(with: UIColor.yellow)
markers.tracksViewChanges = true
markers.map = vwGMap
}
因此,这无法正常工作,并且会产生'属性的错误:属性="坐标";所需类型= NSString;给定类型= nsconcretevalue'。我有几个问题。
如何适当保存坐标数据?我是否将其保存为cllocationDegrees或cllocationCoortion2D?如何将这些对象转换为NSString,还是应该在属性中使用其他类型?(我尝试将其更改为两倍或整数,但也会产生错误)。我有多个位置坐标,我想从核心数据中存储和检索。
如果要处理大量数据,那么Coredata是最好的选择。
这些是我在StorePlace
实体中的属性,它最好仅节省纬度和经度,并在您的实现中从需要时从它们中创建坐标。
@NSManaged public var newPlaceLatitude: Double
@NSManaged public var newPlaceLongitude: Double
和插入新数据,我会做
class func insert(latitude: Double, longitude: Double) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let managedObjectContext = appDelegate.managedObjectContext
let entity = NSEntityDescription.entity(forEntityName: "StoredPlace", in:managedObjectContext)
let newItem = StoredPlace(entity: entity!, insertInto: managedObjectContext)
newItem.newPlaceLatitude = latitude
newItem.newPlaceLongitude = longitude
do {
try managedObjectContext.save()
} catch {
print(error)
}
}
检索纬度后,经度您可以在实现中使用坐标为
let newItem = getCoordinateFromCoreData() // your retrieval function
let coordinate = CLLocationCoordinate2D(latitude: newItem.latitude, longitude: newItem.longitude)
您需要将坐标转换为double/nsdata类型,然后将其存储在Coredata中。使用nskeyedarachiver和nskeyedunarchiver存储和检索值。Coredata无法直接存储Cllocation对象。
// Convert CLLocation object to Data
let newPlaceLatitudeArchived = NSKeyedArchiver.archivedDataWithRootObject(newPlaceLatitude)
//CoreData Piece
newPlace.setValue(newPlaceLatitudeArchived, forKeyPath: "latitude")
// fetch the latitude from CoreData as Archive object and pass it to unarchive to get the CLLocation
let newPlaceLatitudeUnArchived = NSKeyedUnarchiver.unarchiveObjectWithData(archivedLocation) as? CLLocation
在这种情况下,您需要将您的属性转换为Coredata中的二进制类型。