Data parsing is a necessity in most mobile application development. Theoretically, parsing methods are straightforward and predictable, but in reality, they can be complex and prone to bugs. As an application’s scope is inevitably expanded to consume more data, complications and the opportunity for bugs increase exponentially.
Codable is the combined protocol of Swift’s Decodable and Encodable protocols. Together they provide standard methods of decoding data for custom types and encoding data to be saved or transferred. In this article, I will explain why we should use Codable in our applications moving forward. If you would like to know more about how to implement it, start with Apple's overview of Codable, and then check out Part 2 of this article series.
Prior to the introduction of Codable, data decoding required verbose checks for if the desired keys existed and if those values were of the expected type before setting them as property values on a Swift object. Codable, when used with well-formatted JSON, eliminates almost all of the checks that parsing code requires for returning a data object. As data gets more complicated, we can handle the complexity in various stages to keep our code as minimal and clean as possible.