2016-04-21 35 views
7

允許在CoreData中快速存儲MKPolyline所需的代碼是什麼?如何將MKPolyline屬性存儲爲可以在IOS coredata中用swift轉換的屬性?

因此,例如,如果我有一個我想要保存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? 
} 

問題 - 什麼代碼需要讓這個工作?

(我注意到這篇文章重新Objective-C的,但是我在努力理解/端口/得到這個工作 - How do you store data from NSMutable Array in Core Data?

回答

6

存檔折線對象,並保存到核心數據

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) 
} 
+0

哇。非常感謝。讓我儘快測試,然後接受。順便說一下,應該在iOS更新之間銷售這種方法嗎? – Greg

+0

@Greg是的。這個功能在iOS更新之間是穩定的。我的例子只存檔和取消存檔座標。如果你想從MKPolyline存檔更多的東西,那麼需要添加額外的代碼。 – Ramis

+0

好的謝謝 - 我認爲這是我需要的 – Greg

相關問題