swift-codable

Encode and decode Swift data models across JSON and plist formats using Codable.

Updated Jun 5, 2026
One-click install
npx skills add https://github.com/harshav167/build-ios-apps --skill swift-codable-harshav167
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: swift-codable
Source: https://github.com/harshav167/build-ios-apps/tree/main/skills/swift-codable
Command: npx skills add https://github.com/harshav167/build-ios-apps --skill swift-codable-harshav167

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Encode and decode Swift data models across JSON and plist formats using Codable, providing clear guidance on conformance, key remapping, and safe decoding strategies.

Core Features & Use Cases

  • Automatic Codable conformance for simple structs.
  • Custom CodingKeys to rename keys when API payloads differ from Swift property names.
  • Custom init(from:) and encode(to:) for transformations beyond synthesis.
  • Nested and flattened containers handling to map deeply nested JSON structures.
  • Heterogeneous arrays decoding with discriminated unions and enums.
  • Date and data encoding/decoding strategies for API compatibility.
  • Codable usage with URLSession, SwiftData, and UserDefaults integration.
  • Common pitfalls and best practices to avoid runtime decoding errors.

Quick Start

Define a simple User struct conforming to Codable and decode sample JSON with JSONDecoder.

Frequently Asked Questions about swift-codable

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

FAQPage Schema
How do I decode JSON with different key names than my Swift struct properties?

You can map differing JSON keys to Swift properties using custom CodingKeys. By defining an enum conforming to CodingKeys with explicit raw values, you map API payload fields directly to your struct properties during decoding.

What is the best way to handle deeply nested JSON structures in Swift Codable?

Handling deeply nested JSON requires custom init(from:) implementations using nested containers. This approach allows you to drill into nested JSON structures and flatten them into simple Swift data models during decoding.

How do I decode heterogeneous arrays with discriminated unions in Swift?

Decoding heterogeneous arrays requires custom decoding logic using enums with discriminated unions. You parse a specific identifier key to determine the correct concrete type before decoding the rest of the payload.

Can I use Swift Codable with URLSession for API integrations?

Yes, Codable integrates directly with URLSession for API integrations. You decode API responses using JSONDecoder and encode request bodies with JSONEncoder, applying specific date and data strategies for compatibility.

Why does my Swift Codable decoding fail with a runtime error?

Runtime decoding errors usually occur when JSON keys mismatch your Swift properties or types are incompatible. Implementing explicit CodingKeys, robust error handling, and custom decoding strategies prevents these failures.

Does Swift Codable work with UserDefaults and plist formats?

Yes, Codable works with UserDefaults and plist formats. You can encode Swift data models into plist data and decode them back for local data persistence, using PropertyEncoder and PropertyDecoder.