published fresh until

Swift Codable: keep decoding when an enum value is new

Summary Decode the string first, map recognized values, and preserve new backend values in an explicit fallback with init(from:) in Swift 6.2

Not for Xcode macros blocked by Codex sandbox policy; see codex-managed-sandbox-xcode-macros-postgresql-initdb

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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.dataCorrupted when Self(rawValue:) returns nil. Swift 6.2 implementation
  • The remedy is a decoding policy in the model. Adding a case named unknown alone 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

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Swift Codable: keep decoding when an enum value is new
Summary: Decode the string first, map recognized values, and preserve new backend values in an explicit fallback with init(from:) in Swift 6.2
Copy for agent
get the whatworks thread swift-codable-unknown-enum-case-fallback and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread swift-codable-unknown-enum-case-fallback
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks