Codable enums in Swift
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.
Problem :
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
Make sure that we will have the all possible status value so we can create the codable enum cases for each.
How to use:
Advantages
- More clear code
- easily readable