Swift3 如何在swift中正确编码[CLLocation]?

Swift3 如何在swift中正确编码[CLLocation]?,swift3,encode,cllocation,Swift3,Encode,Cllocation,我正在做一个应用程序,需要在本地存储用户的数据。我阅读了苹果网站上的文档,其中包含了如何编码基本类型的部分信息,如String和Int。但是,当我尝试用[CLLocation]类型编码时,失败了。我在问一些专家是否可以给我一些关于如何在Swift中编码这种类型的提示 这是我关于模型类的代码 import Foundation import UIKit import CoreLocation import os.log import CoreData //route model //store

我正在做一个应用程序,需要在本地存储用户的数据。我阅读了苹果网站上的文档,其中包含了如何编码基本类型的部分信息,如String和Int。但是,当我尝试用[CLLocation]类型编码时,失败了。我在问一些专家是否可以给我一些关于如何在Swift中编码这种类型的提示

这是我关于模型类的代码

import Foundation
import UIKit
import CoreLocation
import os.log
import CoreData

//route model
//store the model in the local database.
//change all the [CLLocations] to become the String inorder to store it in local.???? not willing
class Route:  NSObject, NSCoding {
    //MARK: Properties
     var name :String
     var area : String
     var image: UIImage?
     var date : DispatchTime
     var routePoints = [CLLocation]()
     var rating: Int

     //MARK: Achieving paths
    //static properties, belong to the
    static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
    static let ArchiveURL = DocumentsDirectory.appendingPathComponent("Routes")

    //MARK: NSCoding
    func encode(with aCoder: NSCoder) {
        aCoder.encode(name, forKey: PropertyKey.name)
        aCoder.encode(image, forKey: PropertyKey.image)
        aCoder.encode(date, forKey: PropertyKey.date)
        aCoder.encode(area, forKey: PropertyKey.area)
        //unable to encode the [cllocation]
        aCoder.encode(routePoints, forKey: PropertyKey.routePoints)
        aCoder.encode(rating, forKey: PropertyKey.rating)
    }
    //should also added in the locations as one of the variable.
    init?(Name: String, Area: String, Date: DispatchTime, Image: UIImage?, RoutePoints: [CLLocation], Rating: Int) {
        guard (Rating >= 0) && (Rating <= 5) else {
            return nil
        }
        self.name = Name
        self.area = Area
        self.image = Image
        self.date = Date
        self.routePoints = RoutePoints
        self.rating = Rating
    }



    required convenience init?(coder aDecoder: NSCoder){
        //this is the necessary property
        //these are optional properties
        let rating = aDecoder.decodeInteger(forKey: PropertyKey.rating)
       guard   let date = aDecoder.decodeObject(forKey: PropertyKey.date) as? DispatchTime,
        let name = aDecoder.decodeObject(forKey: PropertyKey.name) as? String,
        let image = aDecoder.decodeObject(forKey: PropertyKey.image) as? UIImage,
        let area = aDecoder.decodeObject(forKey: PropertyKey.area) as? String,
        let routePoints = aDecoder.decodeObject(forKey: PropertyKey.routePoints) as? [CLLocation] else{
            print("Unable to decode")
            return nil
        }
        self.init(Name: name, Area: area, Date: date, Image: image, RoutePoints: routePoints, Rating: rating)

    }



    //MARK: Types
    struct PropertyKey {
        static let name = "name"
        static let image = "image"
        static let date = "date"
        static let area = "area"
        static let rating = "rating"
        static let routePoints = "routePoints"
    }


}
<代码>导入基础 导入UIKit 导入核心定位 导入操作系统日志 导入CoreData //路径模型 //将模型存储在本地数据库中。 //将所有[CLLocations]更改为字符串,以便将其存储在本地。????不愿意 类路由:NSObject,NSCoding{ //马克:财产 变量名称:String 变量区域:字符串 var图像:UIImage? var日期:调度时间 var routePoints=[CLLocation]() 风险值评级:整数 //马克:实现路径 //静态属性,属于 静态let DocumentsDirectory=FileManager().url(用于:.documentDirectory,位于:.userDomainMask中)。首先! 静态let ArchiveURL=DocumentsDirectory.appendingPathComponent(“路由”) //标记:NSCoding func编码(带aCoder:NSCoder){ aCoder.encode(名称,forKey:PropertyKey.name) aCoder.encode(image,forKey:PropertyKey.image) aCoder.encode(日期,forKey:PropertyKey.date) aCoder.encode(区域,forKey:PropertyKey.area) //无法对[cllocation]进行编码 aCoder.encode(routePoints,forKey:PropertyKey.routePoints) aCoder.encode(评级,forKey:PropertyKey.rating) } //还应作为变量之一添加到位置中。 初始化?(名称:字符串,区域:字符串,日期:DispatchTime,图像:UIImage?,路由点:[CLLocation],等级:Int){
guard(Rating>=0)和&(Rating原因是CLLocation不能与NSCoder一起存储。您可以使用此类值的swift映射实现一个简单的编码器/解码器,基本上将位置存储在字典中

let p1 = CLLocation(latitude: 1, longitude: 1)
let p2 = CLLocation(latitude: 2, longitude:2)

var locations = [p1, p2]

let codedArray = locations.map{ ["lat":$0.coordinate.latitude, "long":$0.coordinate.longitude] }
let decodedArray = codedArray.map{ CLLocation(latitude:$0["lat"]!, longitude:$0["long"]!) }

您好,但是试过之后,仍然不起作用。我应该对代码数组进行编码吗?