How to decode with default case for enum in Swift

Issue #634

1
2
3
4
5
6
7
8
9
10
public enum Weapon: String, Decodable {
case sword = "SWORD"
case gun = "GUN"
case unknown = "UNKNOWN"

public init(from decoder: Decoder) throws {
let rawValue = try decoder.singleValueContainer().decode(String.self)
self = Weapon(rawValue: rawValue) ?? .unknown
}
}

Comments