Extensions for Codable in Swift
Since we got Codable
protocol in use to go from JSON
data to object of actual Swift struct
or class
.
We use JSONEncoder()
and JSONDecoder()
to do so.
Here I’ve two extensions of Encodable
and Decodable
protocols, that I generally use while in my coding. That enables me to use Codable
Api in very efficient and useful manner.
Here is the full code that can be tested on Playground. 👇
https://gist.github.com/6694e1ad782e1b2c08f054be3eea3243.git
I’ve created decoded() as a generic method that either can infer the type from current context or you can provide whilst making call.
👩💻 !!! KEEP CODING !!! 👨💻
Thank you 😊 for reading. Questions? Leave them in the comment.