Codable
is a protocol that can convert a Swift object itself into and out of Data
type. Codable
is a type alias for Encodable
and Decodable
protocols. It is an easy to use official protocol for parsing JSON
object from server to Swift object.
Encodable
is used when Swift object has to be serialised and sent to server. On the other hand, Decodable
is used when server JSON data is required to be deserialised.
Moreover, custom object conforming to Codable
can now be saved to and retrieved from UserDefaults
directly with only 3 steps. It saves a lot of boilerplate codes.
Let’s get started!
#codable #swift-programming #json #swift #ios