2017-03-16 37 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