
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- Repository
- dpearson2699/swift-ios-skills1.3k
- 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
- Basic Conformance
- Custom CodingKeys
- Custom Decoding and Encoding
- Nested and Flattened Containers
- Heterogeneous Arrays
- Date Decoding Strategies
- Data and Key Strategies
- Lossy Array Decoding
- Single Value Containers
- Default Values for Missing Keys
- Encoder and Decoder Configuration
- Codable with URLSession
- Codable with SwiftData
- Codable with UserDefaults
- Common Mistakes
- Review Checklist
- References
Decode and Verify Workflow
- Decode representative success, missing, null, malformed, acronym-key, and date fixtures.
- On failure, inspect
DecodingError, itscodingPath, and the raw payload. - Correct only the mismatched model, key, container, or strategy; do not hide contract failures with lossy decoding.
- 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)
