It's now easier with Swift 4 to encode / decode to and from JSON or Properties list.
But I can't find how to encode to Data using Codable, without using Objective-C methods initWithCoder
and encodeWithCoder
.
Considering this simple class:
struct Question: Codable {
var title: String
var answer: Int
var question: Int
}
How can I encode it to Data using CodingKeys
and not initWithCoder
and encodeWithCoder
?
EDIT: I also need to be able to deserialize objects previously saved in userdefaults using NSKeyedArchiver.
Well, you no longer need
NSKeyedArchiver
.Try this:
Well it can be achieved via JSONEncoder and JSONDecoder.