Swift Package Index

DictionaryDecoder

https://github.com/swhitty/DictionaryDecoder.git

A Swift library for serializing Codable types to and from [String: Any].


Travis CodeCov Swift 5.0 License Twitter

DictionaryDecoder

A Swift library for serializing Codable types to and from [String: Any] and UserDefaults.

Usage

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)

UserDefaults

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]