menu

SpecFM

A network of designers and developers making the best podcasts, blogs, and resources for designers and developers.

Channels
# All channels
view-forward
# General
view-forward
# Assemble
view-forward
# Design
view-forward
# Design Details
view-forward
# Design Systems
view-forward
# Developer Tea
view-forward
# Does Not Compute
view-forward
# Fragmented
view-forward
# Framework
view-forward
# Immutable
view-forward
# inspect
view-forward
# Jobs
view-forward
# Layout
view-forward
# Music
view-forward
# Orthogonal
view-forward
# Programming
view-forward
# Reactpodcast
view-forward
# Research
view-forward
# Runtime
view-forward
# show-n-tell
view-forward
# Specifics
view-forward
# Swift Unwrapped
view-forward
# Toolsday
view-forward
# Typography
view-forward
Team

#315: Codable Basics πŸ“ 

August 17, 2017 at 7:09pm

#315: Codable Basics πŸ“ 

August 17, 2017 at 7:09pm (Edited 3 years ago)
Swift 4 has brought with it a ton of great improvements. Today we'll cover one of them, it's the new Codable protocol!
Codable ships with Swift 4 as part of the Swift standard library and allows us to conform our own types to it to get encoding and decoding functionality "for free". Encoding and decoding types has never been easier.
Let's dive in!
Before we begin lets back up a second and look at what we're trying to accomplish when it comes to encoding and decoding.
Essentially we want to take an instance of an object or struct and convert (or "encode") it into some other, (usually "machine-readable") format. Then, later, we want to be able to convert (or "decode") the information in that format back into our object or struct.
These days the most common format for encoding/decoding is JSON. It's supported just about everywhere and has the advantage of being both human and machine readable.
For this reason, we'll be discussing JSON in this Bite. It's important to note though, that Codable isn't specific to just JSON. It's built to support encoding and decoding into just about any format we can think of.
In the past, we might have used something like the JSONSerialization class in Foundation to do encode our types into JSON.
This works great, but the encoding input and decoding output was either a Dictionary<String: Any>, or an Array of them. Then we'd need to manually convert to/from our "real" types.
With Codable, we can do much better! Image Image
No messages yet