Master serialization and deserialization in Swift with Codable. Use when implementing custom CodingKeys, date/data decoding strategies, polymorphic/mixed collections, lossy array decoding, property wrappers for default values, or bridging Codable with SwiftData/CoreData persistence.
85%
Does it follow best practices?
Run evals on this skill
Adds up to 20 points to the overall score
Passed
No findings from the security scan
Encode and decode Swift types with Codable (Encodable & Decodable). Covers custom keys, date/data strategies, polymorphic decoding, default values, and persistence compatibility. Targets Swift 6.4 / iOS 26+.
Map serialized JSON field names to Swift property names using CodingKey:
struct UserProfile: Codable {
let id: UUID
var displayName: String
var emailAddress: String
enum CodingKeys: String, CodingKey {
case id
case displayName = "display_name"
case emailAddress = "email"
}
}Configure decoder strategies rather than writing manual decoding logic:
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
decoder.dateDecodingStrategy = .iso8601
decoder.dataDecodingStrategy = .base64
let profiles = try decoder.decode([UserProfile].self, from: jsonData)Note:
.convertFromSnakeCasedoes not handle acronyms well (e.g.server_idbecomesserverId, butserver_urlbecomesserverUrlnotserverURL). Use explicitCodingKeysfor non-standard mappings.
When custom transformations are needed, implement init(from:) and encode(to:):
struct BoundedScore: Codable {
let score: Int
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let raw = try container.decode(Int.self)
self.score = min(max(raw, 0), 100)
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(score)
}
}"type") from a parent container, then decode the corresponding concrete type.See references/codable-advanced-patterns.md for complete implementations.
SwiftData @Model classes can store complex structures that conform to Codable. When persisting Codable structs:
CodingKeys, leading to double-conversion mismatches..iso8601 or .millisecondsSince1970.DecodingError.keyNotFound, .typeMismatch, and .valueNotFound details for exact debugging.init(from:) where possibledateDecodingStrategy and keyDecodingStrategy configured on decoderthienqo/ios-skills
6bf8b62
Canonical home
since May 23, 2026
Also appears in
on Oct 29, 2026
since May 22, 2026
If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.