Getting Started With Codable In Swift 4

Paris Xavier Pinkney
1 min readJan 28, 2019

--

Photo by Steve Johnson on Unsplash

This blog post can also be read on my portfolio site at PXPGraphics.com.

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:

/// 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!

--

--

Paris Xavier Pinkney

Twitter: @pxpgraphics. Senior iOS Engineer + Designer (@udemy, @scribd, @vlnrable, @marqeta). Son + Brother. OAK born. LAS raised. FLG educated. CALI native.