Skip to content
This repository was archived by the owner on Aug 26, 2022. It is now read-only.

Commit fb30184

Browse files
authored
v1.2.1 (#27)
Fix a bug where non-dectionary Codable types where not stored in SingleUserDefaultsStore
1 parent b5a8d45 commit fb30184

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

Sources/SingleUserDefaultsStore.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,15 @@ open class SingleUserDefaultsStore<T: Codable> {
5454
/// - Parameter object: object to save.
5555
/// - Throws: JSON encoding error.
5656
public func save(_ object: T) throws {
57-
let data = try encoder.encode(object)
57+
let data = try encoder.encode(generateDict(for: object))
5858
store.set(data, forKey: key)
5959
}
6060

6161
/// Get object from store. _O(1)_
6262
public var object: T? {
6363
guard let data = store.data(forKey: key) else { return nil }
64-
return try? decoder.decode(T.self, from: data)
64+
guard let dict = try? decoder.decode([String: T].self, from: data) else { return nil }
65+
return extractObject(from: dict)
6566
}
6667

6768
/// Delete object from store. _O(1)_
@@ -71,9 +72,17 @@ open class SingleUserDefaultsStore<T: Codable> {
7172

7273
}
7374

74-
// MARK: - Keys
75+
// MARK: - Helpers
7576
private extension SingleUserDefaultsStore {
7677

78+
func generateDict(for object: T) -> [String: T] {
79+
return ["object": object]
80+
}
81+
82+
func extractObject(from dict: [String: T]) -> T? {
83+
return dict["object"]
84+
}
85+
7786
/// store key for object.
7887
var key: String {
7988
return "\(uniqueIdentifier)-single-object"

0 commit comments

Comments
 (0)