Update: I recently released Disk, a sweet little framework that makes persistence on iOS super easy. I’d love for you to check it out!

With the release of Swift 4, Networking with Foundation ’s built-in classes has never been easier, and structs are even more robust with their ability to encode/decode to and from JSON using the new Codable protocol.

Until now there have generally been only three ways to persist data on iOS:

UserDefaults — simple key-value storage, preferably to be used for any user settings or app states.

NSKeyedArchiver/NSKeyedUnarchiver — provides a way to encode objects into an architecture-independent format that can be stored in a file.

Core Data (or Realm if that’s your thing) —object graph and persistence framework using model files with entities and relationships to represent complex data structures.

So when Apple introduced the new JSON encoding/decoding implementation in Swift 4, I was excited for several reasons:

With the rise of JSON and REST APIs, many iOS apps have become heavily reliant on dealing with JSON data in one way or another. Swift 4 simplifies the process of converting server responses to usable data structures by tenfold.

Converting swift data structures to JSON data has never been easier, allowing developers to store JSON data to disk or encode it into a URLRequest ’s httpBody.

’s httpBody. No more having to use NSObjects that conform to NSCoding with encode(with aCoder: NSCoder) or required convenience init?(coder aDecoder: NSCoder) whenever you want to save a data structure to disk. All that comes free with the Codable protocol.

Getting Started

In this example we will be using JSONPlaceholder, a fake online REST API for developers to toy around with for free. We can make a GET request for some user posts that returns JSON that look like this:

In Swift 4, we have the ability to convert this JSON to a struct, as long as that struct conforms to Codable . After looking at the JSON data structure in the snippet above, we can create a struct representation of a post fairly easily:

It’s important that these property names match the names in our API’s JSON structure. If you don’t use certain properties in your struct that are included in the JSON, that’s fine, but if you want to use a different property name in your struct than in the JSON, you’ll need to include an enum that specifies these property names in your struct, like so:

Now let’s make a simple networking abstraction for retrieving user posts from our API:

… and then let’s implement this abstraction somewhere in an interface managing class:

Now that we’ve retrieved an array of Posts, we might want to persist them on the user’s disk. Saving Codable structs is as easy as using JSONEncoder to encode structs to JSON data and saving that data to a file somewhere on disk. To retrieve that saved data, we’d simply read the JSON data from that file on disk and decode it back to our struct using JSONDecoder .

Or if you want to make things easier for yourself, use Disk, a framework for working with the file system:

And that’s the bare bone basics of getting started with networking and persistence of JSON data in Swift 4.

To learn how to make a POST request with structs and URLSession , and other methods of persisting structs, head over to Part 2!

Please don’t hesitate to reach out to me on Twitter if you need any help or wanna chat!