codable
How to conform UIImage to Codable?
Properly the easiest way is to just make the property Data instead of UIImage like this: public struct SomeImage: Codable { public let photo: Data public init(photo: UIImage) { self.photo = photo.pngData()! } } Deserialize the image: UIImage(data: instanceOfSomeImage.photo)!
Swift Codable Decode Manually Optional Variable
Age is optional: let age: String? So try to decode in this way: let age: String? = try values.decodeIfPresent(String.self, forKey: .age)
Codable ‘has no initializers’ in Xcode 9.3 (Swift 4.1)
As mentioned in the comments, I had to do two things: changing Compilation Mode to Whole Module inside Project settings/Build Settings: reordering the files under Project settings/Build Phases/Compile Sources. Specifically, I brought the files that had an error to the front of the list. Protip: if you search for the name of the file and … Read more
Swift String escaping when serializing to JSON using Codable
You can use .withoutEscapingSlashes option to json decoder to avoid escaping slashes let user = User(username: “John”, profileURL: “http://google.com”) let jsonEncoder = JSONEncoder() jsonEncoder.outputFormatting = .withoutEscapingSlashes let json = try? jsonEncoder.encode(user) if let data = json, let str = String(data: data, encoding: .utf8) { print(str) } Console O/P {“profileURL”:”http://google.com”,”username”:”John”} NOTE: As mention by Martin R … Read more
Implementing a custom Decoder in Swift 4
I haven’t had a chance to turn my code into a framework yet, but you can take a look at my Github Repository that implements both a custom decoder and encoder for XML. Link: https://github.com/ShawnMoore/XMLParsing The encoder and decoder resides in the XML folder of the repo. It is based on Apple’s JSONEncoder and JSONDecoder … Read more
Swift 4 Decodable – Dictionary with enum as key
The problem is that Dictionary‘s Codable conformance can currently only properly handle String and Int keys. For a dictionary with any other Key type (where that Key is Encodable/Decodable), it is encoded and decoded with an unkeyed container (JSON array) with alternating key values. Therefore when attempting to decode the JSON: {“dictionary”: {“enumValue”: “someString”}} into … Read more
Using JSONEncoder to encode a variable with Codable as type
Use a generic type constrained to Encodable func saveObject<T : Encodable>(_ object: T, at location: String) { //Some code let data = try JSONEncoder().encode(object) //Some more code }
Swift 4 Codable; How to decode object with single root-level key
Ollie’s answer is definitely the best way to go for this case, but it does push some knowledge into the caller, which may be undesirable. It also isn’t very flexible. I still think it’s a great answer and exactly what you want here, but this is a nice simple example to explore custom structural encoding. … Read more
What is difference between optional and decodeIfPresent when using Decodable for JSON Parsing?
There’s a subtle, but important difference between these two lines of code: // Exhibit 1 foo = try container.decode(Int?.self, forKey: .foo) // Exhibit 2 foo = try container.decodeIfPresent(Int.self, forKey: .foo) Exhibit 1 will parse: { “foo”: null, “bar”: “something” } but not: { “bar”: “something” } while exhibit 2 will happily parse both. So in … Read more