[英]How to store an MKPolyline attribute as transformable in IOS coredata with swift?
允許在swift中在CoreData中存儲MKPolyline所需的代碼是什么?
因此,例如,如果我有一個我想要保存MKPolyline的核心數據實體(比如說“myEntity”),並將“折線”字段添加為可轉換的,並將其設置為xcode中的“可轉換”。 還生成了NSManagedObject子類。
myEntity.swift
import UIKit
import CoreData
import MapKit
class myEntity: NSManagedObject {
}
myEntity所+ CoreDataProperties.swift
import Foundation
import CoreData
extension myEntity {
@NSManaged var title: String
@NSManaged var polyline: NSObject?
}
問題 - 允許此功能需要哪些代碼?
(我確實注意到這篇文章的目標-c,但是我很難理解/移植/使其工作 - 如何在Core Data中存儲來自NSMutable Array的數據? )
存檔折線對象並保存到Core Data :
let context = self.fetchedResultsController.managedObjectContext
let entity = self.fetchedResultsController.fetchRequest.entity!
let newManagedObject = NSEntityDescription.insertNewObjectForEntityForName(entity.name!, inManagedObjectContext: context)
let polylineObj = polyline() // For test purpose.
let polylineData = polylineToArchive(polylineObj)
newManagedObject.setValue(polylineData, forKey: "polyline")
context.save()
從NSManagedObject取消歸檔折線:
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as! NSManagedObject
let data = object.valueForKey("polyline") as! NSData
let polyline = polylineUnarchive(data)
log(polyline!)
MKPolyline arhiver和unarchiver功能。 還有一些輔助功能。
func polylineUnarchive(polylineArchive: NSData) -> MKPolyline? {
guard let data = NSKeyedUnarchiver.unarchiveObjectWithData(polylineArchive),
let polyline = data as? [Dictionary<String, AnyObject>] else {
return nil
}
var locations: [CLLocation] = []
for item in polyline {
if let latitude = item["latitude"]?.doubleValue,
let longitude = item["longitude"]?.doubleValue {
let location = CLLocation(latitude: latitude, longitude: longitude)
locations.append(location)
}
}
var coordinates = locations.map({(location: CLLocation) -> CLLocationCoordinate2D in return location.coordinate})
let result = MKPolyline(coordinates: &coordinates, count: locations.count)
return result
}
func polylineToArchive(polyline: MKPolyline) -> NSData {
let coordsPointer = UnsafeMutablePointer<CLLocationCoordinate2D>.alloc(polyline.pointCount)
polyline.getCoordinates(coordsPointer, range: NSMakeRange(0, polyline.pointCount))
var coords: [Dictionary<String, AnyObject>] = []
for i in 0..<polyline.pointCount {
let latitude = NSNumber(double: coordsPointer[i].latitude)
let longitude = NSNumber(double: coordsPointer[i].longitude)
let coord = ["latitude" : latitude, "longitude" : longitude]
coords.append(coord)
}
let polylineData = NSKeyedArchiver.archivedDataWithRootObject(coords)
return polylineData
}
func polyline() -> MKPolyline {
let locations = [CLLocation(latitude: 37.582691, longitude: 127.011186), CLLocation(latitude: 37.586112,longitude: 127.011047), CLLocation(latitude: 37.588212, longitude: 127.010438)]
var coordinates = locations.map({(location: CLLocation) -> CLLocationCoordinate2D in return location.coordinate})
let polyline = MKPolyline(coordinates: &coordinates, count: locations.count)
return polyline
}
func log(polyline: MKPolyline) {
let coordsPointer = UnsafeMutablePointer<CLLocationCoordinate2D>.alloc(polyline.pointCount)
polyline.getCoordinates(coordsPointer, range: NSMakeRange(0, polyline.pointCount))
var coords: [Dictionary<String, AnyObject>] = []
for i in 0..<polyline.pointCount {
let latitude = NSNumber(double: coordsPointer[i].latitude)
let longitude = NSNumber(double: coordsPointer[i].longitude)
let coord = ["latitude" : latitude, "longitude" : longitude]
coords.append(coord)
}
print(coords)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.