Skip to content

Instantly share code, notes, and snippets.

@libdx
Last active April 24, 2019 09:33
Show Gist options
  • Save libdx/b7af9ef968e73a27fa547be1a2c30eab to your computer and use it in GitHub Desktop.
Save libdx/b7af9ef968e73a27fa547be1a2c30eab to your computer and use it in GitHub Desktop.
protocol Identifiable {
var id: String { get }
}
struct Settings: Codable, Identifiable {
var id: String = "44"
var name: String = "test"
}
extension Settings: UserDefaultsStoring {}
// aka Storable
protocol UserDefaultsStoring {
static func load(id: String) throws -> Self?
func save() throws
}
extension UserDefaultsStoring where Self: Codable, Self: Identifiable {
static func load(id: String) throws -> Self? {
let defaults = UserDefaults.standard
let key = "\(self).\(id)"
print("load key: \(key)")
if let data = defaults.data(forKey: key) {
let decoder = PropertyListDecoder()
return try decoder.decode(self, from: data)
} else {
return nil
}
}
func save() throws {
let encoder = PropertyListEncoder()
let data = try encoder.encode(self)
let defaults = UserDefaults.standard
let key = "\(type(of: self)).\(id)"
print("save key: \(key)")
defaults.set(data, forKey: key)
}
}
func sample() {
let s1 = Settings()
try! s1.save()
let s2 = try! Settings.load(id: "44")
print(s2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment