How to make enum Decodable in Swift: complete guide
Hi everyone! I'm a beginner iOS developer, and I'm having trouble decoding JSON into an enum.
I have this enum for order status:
```swift
enum OrderStatus: String {
case pending
case confirmed
case shipped
case delivered
case cancelled
}
```
And I receive JSON from the server:
```json
{
"id": 1234,
"status": "pending"
}
```
I'm trying to decode it using JSONDecoder:
```swift
struct Order: Decodable {
let id: Int
let status: OrderStatus
}
```
But Xcode gives an error: "Type 'OrderStatus' does not conform to protocol 'Decodable'"
I already added String as rawValue for the enum, thinking it would help. But no. What am I doing wrong? Do I need to write my own implementation of init(from:) or is there a simpler way?
No answers yet.
Log in to leave an answer