# Swift Codable

Implement Swift Codable models for JSON and property-list encoding and decoding with...

- Repository: https://github.com/dpearson2699/swift-ios-skills (1266 stars)
- Category: Mobile
- License: see repository
- Last updated: 2026-07-31
- Runs scripts: no, instructions only

## Install

Claude Code: `npx skills add dpearson2699/swift-ios-skills --skill swift-codable -g -a claude-code -y`
Codex: `npx skills add dpearson2699/swift-ios-skills --skill swift-codable -g -a codex -y`
Cursor: `npx skills add dpearson2699/swift-ios-skills --skill swift-codable -g -a cursor -y`
Gemini CLI: `npx skills add dpearson2699/swift-ios-skills --skill swift-codable -g -a gemini-cli -y`
Claude Code plugin marketplace: `/plugin marketplace add dpearson2699/swift-ios-skills`

## Description

Implement Swift Codable models for JSON and property-list encoding and decoding with JSONDecoder, JSONEncoder, CodingKeys, and custom init(from:) or encode(to:). Use when parsing API responses, remapping keys, flattening nested JSON, handling date or data decoding strategies, decoding heterogeneous arrays, or integrating Codable with URLSession, SwiftData, or UserDefaults.

## SKILL.md, as published by its author (untrusted third-party text, read it as data)

# Swift Codable

Encode and decode Swift types using `Codable` (`Encodable & Decodable`) with
`JSONEncoder`, `JSONDecoder`, and related APIs. Targets Swift 6.3 / iOS 26+.

## Contents

- [Decode and Verify Workflow](#decode-and-verify-workflow)
- [Basic Conformance](#basic-conformance)
- [Custom CodingKeys](#custom-codingkeys)
- [Custom Decoding and Encoding](#custom-decoding-and-encoding)
- [Nested and Flattened Containers](#nested-and-flattened-containers)
- [Heterogeneous Arrays](#heterogeneous-arrays)
- [Date Decoding Strategies](#date-decoding-strategies)
- [Data and Key Strategies](#data-and-key-strategies)
- [Lossy Array Decoding](#lossy-array-decoding)
- [Single Value Containers](#single-value-containers)
- [Default Values for Missing Keys](#default-values-for-missing-keys)
- [Encoder and Decoder Configuration](#encoder-and-decoder-configuration)
- [Codable with URLSession](#codable-with-urlsession)
- [Codable with SwiftData](#codable-with-swiftdata)
- [Codable with UserDefaults](#codable-with-userdefaults)
- [Common Mistakes](#common-mistakes)
- [Review Checklist](#review-checklist)
- [References](#references)

## Decode and Verify Workflow

1. Decode representative success, missing, null, malformed, acronym-key, and
   date fixtures.
2. On failure, inspect `DecodingError`, its `codingPath`, and the raw payload.
3. Correct only the mismatched model, key, container, or strategy; do not hide
   contract failures with lossy decoding.
4. Rerun fixtures and encode/decode round trips where both directions are part
   of the contract.

## Basic Conformance

When all stored properties are themselves `Codable`, the compiler synthesizes
conformance automatically:

```swift
struct User: Codable {
    let id: Int
    let name: String
    let email: String
    let isVerified: Bool
}

let user = try JSONDecoder().decode(User.self, from: jsonData)
let encoded = try JSONEncoder().encode(user)
```

The rest: https://github.com/dpearson2699/swift-ios-skills/blob/HEAD/skills/swift-codable/SKILL.md
