如何存储一个MKPolyline属性作为可转换的iOS coredata与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? }
问题 – 需要哪些代码才能使其工作?
(我注意到这个post重新objective-c,但我很努力理解/端口/让这个工作 – 你如何存储在核心数据NSMutablearrays的数据? )
归档折线对象并保存到核心数据 :
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函数。 以及一些帮手function。
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) }
- iOS CoreData NSPredicate一次查询多个属性
- 在使用UIManagedDocument的Xcode 4.5的iPhone模拟器5.1中的错误
- 核心数据错误133020:保存中的问题合并:
- NSPredicate与AND和OR问题
- Swift:通过sorting描述符sorting数组
- CoreData:警告:无法为实体“Readings”加载名为“WEEReadings”的类。 找不到类,使用默认的NSManagedObject来代替
- 怪异的核心数据反应 – 不检测NSManagedObject子类
- RestKit – fetchFromDataStore(caching)问题
- MagicalRecords从JSON / NSDictionary导入数据。 导入关系