A Swift library for serializing Codable
types to and from [String: Any]
and UserDefaults
.
🚨This library has been moved to KeyValueCoder
struct Person: Codable {
var name: String
var age: Int
}
// Decode from [String: Any]
let person = try DictionaryDecoder().decode(
Person.self,
from: ["name": "Herbert", "age": 99]
)
// Encode to [String: Any]
let dict = try DictionaryEncoder().encode(person)
Store and retrieve any Codable
type within UserDefaults.
let person = Person(name: "Herbert", age: 99)
// Persist values
try UserDefaults.standard.encode(person, forKey: "owner")
// Retrieve values
let owner = try UserDefaults.standard.decode(Person.self, forKey: "owner")
Types are persisted in a friendly [String: Any]
representation;
let defaults = UserDefaults.standard.dictionaryRepresentation()
let owner = defaults["owner"]
// owner == ["name": "Herbert", "age": 99]