2017-03-16 1 views
0

사용자의 데이터를 로컬에 저장해야하는 응용 프로그램을 만들고 있습니다. Apple 웹 사이트의 설명서를 읽었을 때 String 및 Int와 같은 기본 유형을 인코딩하는 방법에 대한 정보의 일부를 다룹니다. 그러나 [CLLocation] 형식으로 인코딩하려고하면 오류가 발생합니다. 어떤 전문가가 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

관련 문제