Kordian Ledzion

Sep 20, 2017 - 

3 min read  - 

Codable – serialization made easy

codable

With Swift 4 and iOS 11 bringing us a bunch of new solutions and XCode 9 ready to send apps to the AppStore, we have to brace ourselves with the latest technology available to bring users the best experience possible. One of the ways we can achieve it is to make products easier to code, read and maintain. This may also result in faster creation process and quicker reaction to community. Today, I want to write about one little thing you can do to be a step closer to ultimate efficiency – Codable protocol.

I’m pretty convinced that if you’re here, you probably know something about serialization and JSON. While one can spend a great amount of time explaining what Codable is and how it works, I will focus on one thing that may not be that easy to deal with – enums with associated value. Let’s say we want to give non-iPhone-X users an opportunity to send animated emojis. We need to handle enum `Animojis` that takes under consideration real animojis sent from iPhone X and normal emojis that the user wants us to animate with our high-end technology created for the sole purpose of simulating movement of cartoon-ish icons.

Pretty basic, right? Writing custom parser should be an easy task but… hey! With Swift 4 and iOS 11 we don’t want to do that! Let’s focus on encoding first. To do this, we must provide a Encodable struct that will hold associated values for enum’s cases. Let’s start with not-iPhone-X case.

Now we can implement Encodable protocol and use encode function that is available because all properties of the struct are of types that are supported by Codable protocol out of the box.

Now you can make use of Apple’s JSONEncoder to create pretty JSONs! Let’s try it out.

If we print json variable we can see:

There’s no explicit indication of what case this really is, so we would have to put a lot of effort to Decode JSON, especially for more intermediate scenarios. Have to apply some fixes.

Let’s peek what’s under json variable.

That’s better. It’s so easy to structure the data we want, and there’s no need to involve any Keys or parsing. But what about Decoding?! JSON is ready to be shared, so we should include decoding as well. To do that, we have to conform Animoji enum to Decodable protocol, and since it’s already conforming to Encodable, we can simply replace it with Codable and add missing init from Decodable protocol and modify the struct. Here’s the result of the changes.

Now, let’s try to Decode the data variable that has been created through encoding.

In Xcode Playgrounds we can see that under result variable there is our beloved :poop: emoji. Success!

I hope this quick read shed some light on how to deal with Codable protocol. There are some other approaches to this problem, like implementing CodingKeys, or extracting values from decoder containers manually, but what caught my attention was the way of dealing with this case described in this post. If you are not familiar with Codable protocol at all, I recommend following this comprehensive guide. I hope you’ve found it somewhat useful / interesting and don’t hesitate to share it or leave feedback / questions down below in the comments.

Similar posts

Karolina Samorek

Oct 6, 2017 - 

5 min read  - 
Deep neural network swift

Jeremi Kaczmarczyk

Sep 29, 2017 - 

6 min read  - 

We Build Products

Startups, Data Science / AI, Web Development, Mobile Development and Product Design.

Contact us