Codable enums in Swift

Shantaram Kokate
1 min readMay 27, 2019

--

We will be looking forward to how to implement the Codable protocol for most popular enums use cases.

Let start with an example where we will have to get the booking status for a booking Id. There is a various status for booking like new request, canceled, accepted, started, delivered, completed and so on.

In response, we will get the following possible JSON.

Response JSON

Problem :

Traditional approach

How can we easily access the above status?

How to make the code more readable?

How to avoid direct value compression?

Solution :

Here we come with a new approach called Codable enums

Codable Enum

Make sure that we will have the all possible status value so we can create the codable enum cases for each.

How to use:

Response JSON

Advantages

  1. More clear code
  2. easily readable

If you like this post, please share and give claps so others can find it 👏👏

You can follow me on Medium for updated articles. Also, connect with me on LinkedIn, Twitter.

If you have any comment, question, or recommendation, feel free to post them in the comment section below!👇

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Shantaram Kokate
Shantaram Kokate

No responses yet

Write a response