如何快速将MKPolyline属性存储为可转换的IOS核心数据?

格雷格

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

因此,例如,如果我有一个我要保存MKPolyline的核心数据实体(例如“ myEntity”),并已将“ polyline”字段添加为可转换,并在xcode中将其设置为“ transformable”。还产生了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的,但是我一直在努力理解/移植/使它起作用-您如何在核心数据中存储来自NSMutable数组的数据?

拉米斯

归档折线对象并保存到核心数据

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何快速将MKPolyline属性存储为可转换的IOS核心数据?

来自分类Dev

快速将实体(核心数据)中的所有属性更新为true

来自分类Dev

核心数据可转换属性(NSArray)为空

来自分类Dev

核心数据:如何快速将大量数组插入核心数据?

来自分类Dev

如何在核心数据中存储快速枚举?

来自分类Dev

数据如何存储在核心数据模型中(属性)

来自分类Dev

数据如何存储在核心数据模型中(属性)

来自分类Dev

如何在iOS的核心数据中存储JSON数据

来自分类Dev

iOS Swift-如何使用核心数据存储数组?

来自分类Dev

将 UIView 子类或 UIBezierPath 存储在核心数据中作为可转换

来自分类Dev

核心数据的可转换属性上的自定义类的示例

来自分类Dev

iOS:属性为双精度时,核心数据无法保存

来自分类Dev

我如何使用存储的整数属性对核心数据中的数组进行排序?

来自分类Dev

如何保存相关的核心数据对象以快速分析

来自分类Dev

如何在核心数据中顺序存储数据?

来自分类Dev

如何在核心数据模型iOS中找出特定属性索引的值

来自分类Dev

如何快速将熊猫数据框行转换为ordereddict

来自分类Dev

如何快速将Pandas数据框转换为元组列表

来自分类Dev

如何快速将 JSON 转换为结构类型数据?

来自分类Dev

iOS-核心数据集属性主键

来自分类Dev

核心数据提取属性

来自分类Dev

关系的核心数据属性?

来自分类Dev

如何在核心数据中存储NSDate?

来自分类Dev

如何在核心数据中存储CGPoint?

来自分类Dev

如何在核心数据中存储数组(Swift)

来自分类Dev

如何在核心数据中存储NSDate?

来自分类Dev

如何在核心数据中存储UIButton值?

来自分类Dev

如何将图像从collectionviewcell存储到核心数据

来自分类Dev

如何从核心数据表iOS中检索数据?

Related 相关文章

  1. 1

    如何快速将MKPolyline属性存储为可转换的IOS核心数据?

  2. 2

    快速将实体(核心数据)中的所有属性更新为true

  3. 3

    核心数据可转换属性(NSArray)为空

  4. 4

    核心数据:如何快速将大量数组插入核心数据?

  5. 5

    如何在核心数据中存储快速枚举?

  6. 6

    数据如何存储在核心数据模型中(属性)

  7. 7

    数据如何存储在核心数据模型中(属性)

  8. 8

    如何在iOS的核心数据中存储JSON数据

  9. 9

    iOS Swift-如何使用核心数据存储数组?

  10. 10

    将 UIView 子类或 UIBezierPath 存储在核心数据中作为可转换

  11. 11

    核心数据的可转换属性上的自定义类的示例

  12. 12

    iOS:属性为双精度时,核心数据无法保存

  13. 13

    我如何使用存储的整数属性对核心数据中的数组进行排序?

  14. 14

    如何保存相关的核心数据对象以快速分析

  15. 15

    如何在核心数据中顺序存储数据?

  16. 16

    如何在核心数据模型iOS中找出特定属性索引的值

  17. 17

    如何快速将熊猫数据框行转换为ordereddict

  18. 18

    如何快速将Pandas数据框转换为元组列表

  19. 19

    如何快速将 JSON 转换为结构类型数据?

  20. 20

    iOS-核心数据集属性主键

  21. 21

    核心数据提取属性

  22. 22

    关系的核心数据属性?

  23. 23

    如何在核心数据中存储NSDate?

  24. 24

    如何在核心数据中存储CGPoint?

  25. 25

    如何在核心数据中存储数组(Swift)

  26. 26

    如何在核心数据中存储NSDate?

  27. 27

    如何在核心数据中存储UIButton值?

  28. 28

    如何将图像从collectionviewcell存储到核心数据

  29. 29

    如何从核心数据表iOS中检索数据?

热门标签

归档