SwiftDictionaryCoding icon indicating copy to clipboard operation
SwiftDictionaryCoding copied to clipboard

Decode / encode instances of a Decodable/Encodable object to / from dictionaries -

SwiftDictionaryCoding

CI Status Version License Platform

This library allows you to use a encode/decode Swift objects that conform to Codable protocols to and from Dictionaries in the same way you'd use a JSONEncoder/Decoder to convert to JSON. They use the same encoding and decoding strategies as their JSON counterparts (because they wrap the JSON equivalents)

Encoding


struct Event: Codable {
    let id: Int
    let location: String?
    let price: Float?
}

let event = Event(id: 1, eventName: "Christmas", location: nil, price: 10)
let encoder = DictionaryEncoder()
let dict = try encoder.encode(event) 

Decoding

let dict: [String: Any] = ["id": 1, "eventName": "Christmas", "price": 10]        
let decoder = DictionaryDecoder()
let event = try decoder.decode(Event.self, from: dict))

Installation

SwiftDictionaryCoding is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'SwiftDictionaryCoding'

Author

ashleymills, [email protected]

License

SwiftDictionaryCoding is available under the MIT license. See the LICENSE file for more info.