We have a custom object setup like so:
struct BallPark: Codable,Equatable {
static func == (lhs: LeanVenue, rhs: LeanVenue) -> Bool {
return lhs.id == rhs.id
}
let id: String
let name: String?
let location: VenueCoordinates?
let mapEnabled: Bool?
let facilityStatus: String?
}
struct VenueCoordinates: Codable {
let latitude: String?
let longitude: String?
var lat: Double? {
guard let latitud = latitude else { return nil}
return Double(latitud)
}
var lon: Double? {
guard let longitude = longitude else { return nil}
return Double(longitude)
}
}
We are trying to convert it to type Data so it can be saved to user defaults like so:
guard let bestBallParkToSave = theBestBallPark // this is of type BallPark, after fetching data
else {return}
let encodedBestBallPark = NSKeyedArchiver.archivedData(withRootObject: bestBallParkToSave)
UserDefaults.standard.set(encodedBestBallPark, forKey: "favoriteBallPark")
The problem is it causes an error when trying to convert:
'NSInvalidArgumentException', reason: '-[__SwiftValue encodeWithCoder:]: unrecognized selector sent to instance 0x6000027dca80'
I've tried using this stack overflow answer: How to convert custom object to Data Swift but there wasn't really a clear answer except making sure it conforms to Codable which I believe my struct and everything in it does. If you have any suggestions please let me know.