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.

View the code on Gist.

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.

View the code on Gist.

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.

View the code on Gist.

If we print json variable we can see:

View the code on Gist.

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.

View the code on Gist.

Let’s peek what’s under json variable.

View the code on Gist.

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.

View the code on Gist.

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

View the code on Gist.

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.