I am working on a project which is mix of Objective C and Swift.
I have some Codable objects which I need to use in Objective C but these are not inherited from NSObject
. If I make them inherit from NSObject
I am not able to convert to json string using JSONEncoder
class Sample:NSObject, Codable
{
var name:String = "Lucien"
var data:[String:String] = ["token":"121bnkfjas"]
}
let sample = Sample()
let jsonData = try JSONEncoder().encode(sample)
let jsonString = String(data: jsonData, encoding: .utf8)!
I have a jsonString
as {}
.
If I make only Confirm to Codable protocol it is working properly.
class Sample:Codable
{
var name:String = "Lucien"
var data:[String:String] = ["token":"121bnkfjas"]
}
let sample = Sample()
let jsonData = try JSONEncoder().encode(sample)
let jsonString = String(data: jsonData, encoding: .utf8)!
This jsonString is proper.
How can I use only Codable objects in Objective C?