0

I'm getting a result and I'm trying to make it JSON serializable, which probably mean to convert it from NSObject to Object. Is that possible? I've checked Codable, but I didn't understand what I'm supposed to do. My result could change in the future (types, keys, values, etc).

dump(result):

{ test1: 123, test2: () }> #0
  - super: NSObject

Edit:

let result = appleIDCredential
let jsonEncoder = JSONEncoder()
let jsonData = try jsonEncoder.encode(result)
let json = String(data: jsonData, encoding: String.Encoding.utf8)

Returns:

Argument type 'ASAuthorizationAppleIDCredential' does not conform to expected type 'Encodable'

1 Answers1

0

You can serialize and deserialize NSObjects with this useful framework. HandyJSON is a framework written in Swift which to make converting model objects( pure classes/structs ) to and from JSON easy. https://cocoapods.org/pods/HandyJSON

Petros
  • 342
  • 3
  • 15
  • Thank you for your suggestion! Is there any way I can serialize/deserialize without any external frameworks? Thanks! –  Dec 16 '19 at 20:37
  • I think it is but in Swift 4. Here is a similar question https://stackoverflow.com/questions/29599005/how-to-serialize-or-convert-swift-objects-to-json – Petros Dec 16 '19 at 20:38
  • I've edited my initial question with the `JSONEncoder` stuff. Now it throws an error `Argument type 'ASAuthorizationAppleIDCredential' does not conform to expected type 'Encodable'`. I guess it's because I didn't make the object Codable, I'm not sure how to do it.. –  Dec 16 '19 at 21:32