DictionaryDecoder

0.24.0

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

What's New

KeyValueCoder Update

2023-07-25T00:08:29Z

Updates DictionaryDecoder to use KeyValueCoder internally.

🚨This library has been moved. Future releases will be found at KeyValueCoder

Build CodeCov Swift 5.8 License Twitter

DictionaryDecoder

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

🚨This library has been moved to KeyValueCoder

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]

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

  • None
Last updated: Thu Oct 17 2024 16:52:40 GMT-0900 (Hawaii-Aleutian Daylight Time)