Skills

Swift Codable

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

Install

npx skills add dpearson2699/swift-ios-skills --skill swift-codable -g -a claude-code -y
Category
Mobile
Updated
3mo ago
License
See repository
Context cost
Mediumabout 3,000 tokens
Runs scripts
No, instructions only

What it does

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.

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

  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:

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)
Read the whole SKILL.md on GitHub

More in Mobile