What this thread does
Implement init(from:) to decode the wire string and map unrecognized strings into an explicit .unknown(String) case.
As of November 2026; checked 2026-11-08. This behavior is anchored to the Swift 6.2 standard library, whose release announcement is dated September 15, 2025.
What changed
- A backend can add a valid new string while an older app's raw-value enum still rejects it. The default implementation throws
DecodingError.dataCorruptedwhenSelf(rawValue:)returns nil. Swift 6.2 implementation - The remedy is a decoding policy in the model. Adding a case named
unknownalone does not make it a catch-all.
Do first: decode the string and preserve unknown values
This example implements both directions so encoding still produces the original single JSON string:
import Foundation
enum DeliveryStatus: Codable {
case queued
case delivered
case unknown(String)
init(from decoder: Decoder) throws {
let raw = try decoder.singleValueContainer().decode(String.self)
switch raw {
case "queued": self = .queued
case "delivered": self = .delivered
default: self = .unknown(raw)
}
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .queued: try container.encode("queued")
case .delivered: try container.encode("delivered")
case .unknown(let raw): try container.encode(raw)
}
}
}
struct Delivery: Codable {
let status: DeliveryStatus
let reference: String
}
An unfamiliar string such as "held_for_review" can now be represented while the enclosing Delivery continues decoding. Decide how the UI displays .unknown; retain the raw value for later interpretation. This illustrative conformance uses the single-value container mechanism explained by Itai Ferber on Swift Forums, August 25, 2020.
For a String raw-value enum needing only a sentinel, a custom initializer can instead assign Self(rawValue: raw) ?? .unknown. That discards the original unrecognized value; use the associated-value approach when preservation matters.
Diagnostic: Cannot initialize Options from invalid String value first
This exact diagnostic appears in Peter Schorn's forum example. Its replies explain that CodingKeys do not remap the contents of a raw-value enum's single JSON string. Put wire spellings in raw values or handle them in a custom decoder.
An optional enum property or decodeIfPresent handles a missing key or null; a present invalid value can still throw. The standard-library contract preserves that distinction.
Limits
The try ...decode(String.self) stays strict: a number, object or null remains a decoding error. This covers future string values, not arbitrary malformed JSON or missing required fields. Avoid wrapping the entire payload in try? when you need to distinguish those failures.
@unknown default addresses switch exhaustiveness for evolving enums; it does not teach a JSON decoder to construct a new case. SE-0192
Not for: Xcode macros blocked by Codex sandbox policy; see the sandbox guide.
Started from a post by @Peter Schorn; Ole Begemann; Itai Ferber, packaged by the editor. Original post