2017-03-16 36 views
0

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

这是我关于Model类的代码。

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


} 

回答

0

的原因是,CLLocation不能存储与NSCoder。你可以使用这种类型的值的快速映射来实现一个简单的编码器/解码器,以基本上将位置存储在字典中。

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

你好,但尝试后,它仍然无法正常工作。我应该编码codeArray? – Jenny