swift-codable

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

Updated May 6, 2026
One-click install
npx skills add https://github.com/Roy-wonji/claude-config --skill swift-codable-roy-wonji
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: swift-codable
Source: https://github.com/Roy-wonji/claude-config/tree/main/skills/swift-ios-skills/skills/swift-codable
Command: npx skills add https://github.com/Roy-wonji/claude-config --skill swift-codable-roy-wonji

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Swift Codable can be hard to get right when JSON keys don’t match your model, dates use different formats, arrays contain mixed types, or encoding/decoding must integrate with networking, persistence, or user defaults.

Core Features & Use Cases

  • Custom key mapping: Use CodingKeys and decoder/encoder key strategies to handle snake_case and renamed fields.
  • Custom decoding/encoding: Implement init(from:) and encode(to:) for transformations like epoch-to-Date conversion and derived fields.
  • Robust decoding patterns: Handle nested/flattened containers, heterogeneous arrays via discriminators, lossy array decoding, and missing-key defaults.
  • Integration-ready: Apply consistent Codable configuration with JSONEncoder/JSONDecoder for URLSession, SwiftData, and UserDefaults.

Use Case: You receive an API response where timestamps are Unix epoch numbers, keys are snake_case, and a single field is an array of mixed “text” and “image” objects; use this Skill to model it cleanly with safe decoding and deterministic encoding for tests.

Quick Start

Use the swift-codable skill to generate Codable models and decoding/encoding logic for your specific JSON shape, including custom key mapping, date parsing strategy, and any heterogeneous or lossy arrays.

Frequently Asked Questions about swift-codable

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I decode a JSON array with mixed object types in Swift Codable?

Decoding heterogeneous arrays in Swift Codable requires discriminator-based decoding with custom init(from:) to identify and map mixed types like text and image objects into a single enum or protocol-based array.

How do I handle snake_case JSON keys when decoding API responses in Swift?

Snake_case JSON keys are handled in Swift Codable by defining CodingKeys to map renamed fields or by applying a keyDecodingStrategy on JSONDecoder to automatically convert snake_case to camelCase properties.

What is the best way to decode Unix epoch timestamps into Date objects using JSONDecoder?

Decoding Unix epoch timestamps into Date objects is achieved by configuring JSONDecoder with a custom dateDecodingStrategy or implementing a custom init(from:) to convert epoch numbers into Date instances.

How do I prevent JSON decoding failures when an API response has missing or partially invalid data?

Missing or invalid data is handled safely in Swift Codable by implementing lossy array decoding, using optional types, and providing default values during custom decoding to skip invalid entries without failing the entire response.

Can I use Swift Codable to flatten nested JSON containers into a single model?

Swift Codable can flatten nested JSON containers by using nestedContainer or singleContainer within a custom init(from:) to extract values from deep JSON structures into top-level model properties.

Does Swift Codable work with URLSession and UserDefaults for data persistence?

Swift Codable works directly with URLSession and UserDefaults by configuring JSONEncoder and JSONDecoder to serialize Swift models into JSON data for network requests and local persistence.