I have a JSON file I am reading from disk
This is the structure of the file:
[
{
"name": "3X",
"priority": 33
},
{
"name": "4X",
"priority": 32
}
]
I am decoding it to a class called Test
class TestClass: NSObject, Codable {
enum CodingKeys: String, CodingKey {
case name, priority
}
let name:String
let priority:Int
var optional:String? = nil
var problem:String = "eli"
init(name:String, priority:Int, optional:String? = nil, problem:String) {
self.name = name
self.priority = priority
self.optional = optional
self.problem = problem
super.init()
}
}
This is the code I am using to decode the file
if let path = Bundle.main.path(forResource: "test", ofType: "json") {
do {
let jsonData = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
let testObjects = try JSONDecoder().decode([TestClass].self, from: jsonData)
let data = try! NSKeyedArchiver.archivedData(withRootObject: testObjects, requiringSecureCoding: false)
UserDefaults.standard.set(data, forKey: "test_objects")
} catch {
// error in parsing json
}
}
After decoding I am trying to save the array into disk, I am converting it into NSData for saving however NSKeyedArchiver.archivedData
fails with the following error:
{NSDebugDescription=Caught exception during archival: -[BugTesting.TestClass encodeWithCoder:]: unrecognized selector sent to instance 0x600002119ac0
To me this looks very strange since I am conforming to Codable
.
Any help would be appreciated.