Swift Codable
Swift Codable teaches you to serialize and deserialize Swift types to and from JSON using the standard Codable protocol. Learn automatic synthesis, custom key mapping, nested container navigation, date and data strategies, and integration patterns with URLSession, SwiftData, and UserDefaults.
Swift Codable helps you implement JSON serialization and deserialization in Swift using Codable, JSONEncoder, and JSONDecoder.
AI-generated summary based on this skill's SKILL.md
Install
dpearson2699/swift-ios-skills/swift-codable · repository language: Python
git clone https://github.com/dpearson2699/swift-ios-skills
cp -r swift-ios-skills ~/.claude/skills/swift-codablegenerated, unverified - the skill's exact subdirectory could not be determined; check the repository on GitHub
Frequently asked questions
AI-generated answers based on this skill's SKILL.md and metadata
How to use Codable in Swift for JSON encoding and decoding?
Swift Codable enables automatic serialization and deserialization of Swift types to and from JSON. To use it, declare your struct or class as conforming to Codable, and Swift synthesizes the required Encoder and Decoder implementations automatically. For example, a struct with String and Int properties marked `Codable` can be encoded to JSON via `JSONEncoder()` and decoded back using `JSONDecoder()`. This eliminates manual parsing boilerplate while maintaining type safety.
What are Swift Codable best practices for production code?
Swift Codable best practices include: use `CodingKeys` enum for custom key mapping when JSON field names differ from your Swift properties; leverage `DateDecodingStrategy` and `DataDecodingStrategy` to handle temporal and binary data; implement custom `init(from:)` and `encode(to:)` only when automatic synthesis is insufficient; validate decoded data in post-init logic; and handle decoding errors gracefully with try-catch blocks. Test edge cases like null values, missing fields, and type mismatches.
How does Swift Codable handle nested objects and arrays?
Swift Codable automatically handles nested structures and arrays through recursive synthesis. Nested types must also conform to Codable. Use `KeyedDecodingContainer` and `UnkeyedDecodingContainer` for custom navigation when needed. For arrays of Codable types, Swift synthesizes array encoding/decoding directly. When JSON structure doesn't match your model, define intermediate wrapper types or use custom `init(from:)` to manually navigate containers and reconstruct your target structure.
What is the Swift Codable protocol and what conformance requires?
Swift Codable is a protocol combining Encodable and Decodable, enabling bidirectional JSON transformation. Conformance requires implementing `encode(to:)` and `init(from:)` methods, though Swift synthesizes these automatically for structs and classes with all-Codable properties and no custom logic. Manual conformance is needed for types with computed properties, custom initialization, or non-standard JSON mappings. The protocol works with Encoder and Decoder implementations like JSONEncoder and JSONDecoder.
How do you solve custom encoding and decoding challenges with Codable?
Swift Codable custom encoding/decoding is handled by implementing `encode(to:)` and `init(from:)` manually. Use `KeyedEncodingContainer` and `KeyedDecodingContainer` to map properties to JSON keys. For non-standard formats, configure `JSONEncoder` and `JSONDecoder` strategies—`dateEncodingStrategy`, `dataEncodingStrategy`, `keyEncodingStrategy`. Handle type mismatches and missing fields by catching `DecodingError`. For complex transformations, create intermediate types or use custom container navigation to reshape data during encoding/decoding.
Can you map custom JSON keys to Swift properties using Codable?
Yes, Swift Codable supports custom key mapping via the `CodingKeys` enum. Define a nested enum conforming to `CodingKey` inside your type, listing each property with its corresponding JSON key as a string raw value. For example, a property `userName` can map to JSON key `user_name` by declaring `case userName = "user_name"` in CodingKeys. This allows your Swift code to follow camelCase conventions while JSON uses snake_case or other formats without manual parsing.