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-667adcc64d6fc373447930/] 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-667adcc64d709495873498/]   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-667adcc64d70f701497145/]   The JSONCodable protocol now has two required methods :

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

Tagged , Read More