Codable synthesis for enums with associated values Swift 4 introduced support for automatically synthesizing conformance to the Encodable and Decodable protocols. It's a useful feature, most likely used by any application which handles JSON-based network requests. This Swift evolution pitch from Dario Rexin proposes extending auto-synthesis of these conformances to enums with associated values. I'm sure we will see this in an upcoming Swift version. Posted on December 8, 2020 #evolution #enum #codable
Parsing Tricky JSON With Codable in Swift Parsing JSON is sometimes not as straightforward as we would like it to be, and we might find ourselves dealing with malformed JSON payloads. In this post, Andrés Ibañez shares his tips for parsing tricky JSON using Swift's Codable protocol. Posted on November 18, 2020 #codable #json
Save Custom Codable Types in AppStorage or SceneStorage AppStorage and SceneStorage are SwiftUI property wrappers designed for data persistence. Both of these property wrappers support Swift value types such as Bool, Int, etc. However, to use these property wrappers with custom codable types, we need to add RawRepresentable conformance to the types. In this post, Natalia Panferova shows us how to do this. Posted on September 8, 2020 #swiftui #codable #ios14
Adding support for versioning and migration to your Codable models. I really like this idea from Krzysztof Zabłocki that adds support for versioning to Codable models. It's super lightweight, involving conformance to a single protocol and a custom decoder with one method, but allows us to decode any versioned model and apply all necessary migrations. Posted on June 16, 2020 #codable
Swift Tip: Codable Enums In this post from Objc.io, we learn how to add Codable conformance to enums which have associated types. A very useful tip. Posted on January 29, 2018 #enum #codable
Swift Codable With Custom Dates Another useful post from Keith Harrison of Use Your Loaf. This time Keith shows us how to use Swift 4’s Codable protocols to decode JSON which contains date values in more than one format. Posted on December 14, 2017 #codable #date
Why Dictionary sometimes encodes itself as an array Oleb Begemann with a very interesting post on why Swift dictionaries sometimes encode themselves as arrays when using Swift 4’s Codable protocols. I wasn’t aware of this limitation, and as he points out, there is no clean and simple fix for it. Posted on December 6, 2017 #codable #enum
SingleValueCodable - A simple exercise in leverage A single-value wrapper type is a type which encapsulates only a single property. Dealing with such types when using Swift’s Codable protocols can lead to less than perfect representations of the type’s data. In this post, Russ Bishop works through a small exercise in extending Codable to better such types. Posted on October 27, 2017 #codable
Codable Enums Swift 4’s Codable protocols make it very easy to encode and decode conforming types, even working with enums when the enums rawValue also conforms to Codable. However, things get a little bit more complicated when dealing with enums which have associated values. This post from Little Bites of Cocoa shows us how we can handle such a situation. Posted on October 7, 2017 #codable #swift 4 #enum
When JSONDecoder meets the real world, things get ugly… Dave Lyon looks at decoding real-world API data using Swift 4’s Codable protocols. He explains why dealing with real-world JSON data might not be as simple as some of the examples we may have seen, and shows us how we can avoid common pitfalls. Posted on October 1, 2017 #codable #swift 4 #json