Codable protocol in Swift 4 + extension

When Swift 4 went out and we finally had a chance to play with it in a playground, I began reading this article on Ray Wenderlich's site :   I was particularly interested in this part about the new Codable protocol : [crayon-5b2c9dc3f0e77423549884/] As you can see, it's a pretty interesting (and simple) way to convert a Codable into a String (a stringified version of a JSON dictionary).   I also read this article from Natasha-The-Robot's weekly newsletter : This is interesting: the author creates a new protocol (Serializable) to let the Codable-conforming object encode itself to Data. [crayon-5b2c9dc3f0e84616745310/]   Well, as far as I'm concerned, I often have to parse a JSON dictionary (a.k.a. a Dictionary<String, Any>), or create a JSON dictionary from an object, not a String or a Data. So, this is my implementation of this Serializable protocol idea : [crayon-5b2c9dc3f0e8a929576691/]   The JSONCodable protocol now has two required methods :

  • a function allowing the Serializable-conforming object to convert itself a JSON

Tagged , Read More