DataConvertible
DataConvertible copied to clipboard
DataConvertible is a support library for converting models to Data.
DataConvertible
DataConvertible is a support library for converting models to Data
.
How to use
Encode / Decode
struct Model: Codable, DataConvertible {
let string: String
let int: Int
}
let model = Model(string: "abc", int: 123)
let data: Data = try model.convertToData()
let decoded: Model = try Model(data: data)
Save to UserDefaults / Get from UserDefaults
let model = Model(string: "abc", int: 123)
try userDefaults.set(model, forKey: "model")
let saved: Model? = userDefaults.value(Model.self, forKey: "model")
Adapt to other data store type
// e.g. Keychain
struct Keychain { /* implementation */ }
extension Keychain: DataConvertibleStore {
public func set(_ value: DataConvertible, forKey key: String) throws {
// store the value
}
public func value<T: DataConvertible>(_ type: T.Type = T.self, forKey key: String) -> T? {
// get the value by the key
}
}
Conform to DataConvertible
extension YourClass: DataConvertible {
init(data: Data) throws {
// initialize form Data
}
func convertToData() throws -> Data {
// convert to data
}
}
If your class conforms to Codable, the class can conform to DataConvertible without any additional implementation.
Installation
Carthage
github "tattn/DataConvertible"
Contributing
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
License
DataConvertible is released under the MIT license. See LICENSE for details.