enum PostType: Decodable {

    init(from decoder: Decoder) throws {

        // What do i put here?
    }

    case Image
    enum CodingKeys: String, CodingKey {
        case image
    }
}

What do i put to complete this?
Also, lets say i changed the case to this:

case image(value: Int)

How do i make this conform to Decodable ?

EDit Here is my full code (which does not work)

let jsonData = """
{
    "count": 4
}
""".data(using: .utf8)!

        do {
            let decoder = JSONDecoder()
            let response = try decoder.decode(PostType.self, from: jsonData)

            print(response)
        } catch {
            print(error)
        }
    }
}

enum PostType: Int, Codable {
    case count = 4
}

Final Edit
Also, how will it handle an enum like this?

enum PostType: Decodable {
    case count(number: Int)
}

9 Answers
9

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *