Getting Started with Codable in Swift 4

Photo by  Steve Johnson  on  Unsplash

This post is the first of a ten-part series called Exploring Codable and Core Data in Swift 4.

What is Codable?

Apple's Foundation framework provides two protocols for encoding and decoding objects in Objective-C: NSCoding and NSSecureCoding. The introduction of Swift 4 has brought us two new protocols to handle data encoding and decoding: Encodable and Decodable. Collectively, these protocols are known as Codable.

Codable is a type alias for the Encodable and Decodable protocols:

Codable Definition
    /// A type that can convert itself into and out of an external representation.
typealias Codable = Decodable & Encodable

Now, what exactly is an external representation anyway? These representations can consist of property lists, JSON, XML, protobuf, and other flat binary representations. For this series, we'll be using JSON because it's a common way of sending or receiving data over a network connections.

In our next post, titled Decoding Custom Types with Decodable (coming soon), we'll discover how to turn JSON objects into Decodable data models automatically and manually in Swift.

Stay tuned!