【问题标题】:How to store an MKPolyline attribute as transformable in IOS coredata with swift?如何使用swift在IOS coredata中将MKPolyline属性存储为可转换的?
【发布时间】:2016-08-14 04:43:17
【问题描述】:

允许在 CoreData 中快速存储 MKPolyline 所需的代码是什么?

例如,如果我有一个核心数据实体(例如“myEntity”),我想为其保存 MKPolyline,并将“折线”字段添加为可转换,并将其设置为“可转换”代码。也产生了 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?

【问题讨论】:

    标签: ios swift core-data nscoding mkpolyline


    【解决方案1】:

    归档折线对象并保存到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 归档和取消归档功能。还有一些辅助函数。

    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更新之间是否应该出售该方法?
    • @Greg 是的。此功能在 iOS 更新之间是可靠的。我的示例仅归档和取消归档坐标。如果您想从 MKPolyline 归档更多内容,则需要添加其他代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 2012-03-17
    • 1970-01-01
    • 2016-01-05
    • 2021-11-22
    相关资源
    最近更新 更多