Consider the following simple JSON:
{
"title": "Some title",
"subtitle": "Some subtitle",
"button_type": "rounded"
}
This is my current approach towards decoding the buttonType field:
// I dont have access to this enums implementation as it comes from a 3rd party lib.
enum ButtonType {
case squared
case simple
case rounded
case normal
}
struct ResponseModel: Decodable {
var title: String
var subtitle: String
var type: ButtonType
enum CodingKeys: String, CodingKey {
case title = "title"
case subtitle = "subtitle"
case type = "button_type"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
title = try container.decode(String.self, forKey: .title)
subtitle = try container.decode(String.self, forKey: .subtitle)
let stringType = try container.decode(String.self, forKey: .type)
switch stringType {
case "squared":
type = .squared
case "simple":
type = .simple
case "rounded":
type = .rounded
default:
type = .normal
}
}
}
Is there any prettier way to accomplish decoding the string to the custom enum without that nasty switch statement iterating over a plain string?. I sadly do not have access to the enums implementation as it comes from a third party library. If I did I would just make the enum conform to String & Codable and have Codable work its magic, but I dont.
Thanks!
2
Answers
You can create your own
enum
Then change
To
And when you need the custom
enum
just useThe
String
after the:
in the enum makes it conform toRawRepresentable<String>
you won’t need the custominit
anymore.Just another solution.
Even if you don’t control the how
ButtonType
is defined, you can still conform it toDecodable
using an extension.Step 1: Conform
ButtonType
toDecodable
Step 2: Use it in your
ResponseModel