I want to save a save a dictionary, but I keep getting this error message: "SwiftValue encodeWithCoder:]: unrecognized selector sent to instance". This is how I try to save the Dictionary:
func savePlaces(){
let placesData = NSKeyedArchiver.archivedData(withRootObject: Resultaat?.reisdelen)
UserDefaults.standard.set(placesData, forKey: "Reis")
}
How I try to load the dictionary:
var Reis = [reisdelen]()
func loadPlaces(){
let placesData = UserDefaults.standard.object(forKey: "Reis") as? NSData
if let placesData = placesData {
let placesArray = NSKeyedUnarchiver.unarchiveObject(with: placesData as Data) as? [reisdelen]
if let placesArray = placesArray {
Reis = placesArray
TableView.reloadData()
}
}
}
My Struct:
struct reismogelijkheden: Decodable {
let reisdelen: [reisdelen]?
let reistijd: reistijd?
let reistijdActueel : reistijdActueel?
let aantalOverstappen: Int?
let vertrektijd: String?
let vertrektijdVertraging: vertrektijdVertraging?
let aankomsttijd: String?
}
struct reisdelen: Decodable {
let vervoerder: String?
let richting: String?
let treinnummer: String?
let lijn: String?
let stops: [stops]?
let modaliteit: modaliteit?
let travelTimeInMinutes: Int?
let travelDistanceInMeters: Int?
}