Skip to content

Instantly share code, notes, and snippets.

View ricardohochman's full-sized avatar

Ricardo Hochman ricardohochman

  • BTG Pactual Digital
View GitHub Profile
@ricardohochman
ricardohochman / OptionalCodableEnum.swift
Last active July 2, 2024 09:04
Safely decode and encode enums in Swift
public struct OptionalCodableEnum<T>: Codable where T: RawRepresentable, T.RawValue: Codable {
public let value: T?
public init(value: T) {
self.value = value
}
public init(from decoder: Decoder) throws {
value = T(rawValue: try decoder.singleValueContainer().decode(T.RawValue.self))
}