This repository was archived by the owner on Aug 26, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +12
-3
lines changed Expand file tree Collapse file tree 1 file changed +12
-3
lines changed Original file line number Diff line number Diff line change @@ -54,14 +54,15 @@ open class SingleUserDefaultsStore<T: Codable> {
54
54
/// - Parameter object: object to save.
55
55
/// - Throws: JSON encoding error.
56
56
public func save( _ object: T ) throws {
57
- let data = try encoder. encode ( object)
57
+ let data = try encoder. encode ( generateDict ( for : object) )
58
58
store. set ( data, forKey: key)
59
59
}
60
60
61
61
/// Get object from store. _O(1)_
62
62
public var object : T ? {
63
63
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)
65
66
}
66
67
67
68
/// Delete object from store. _O(1)_
@@ -71,9 +72,17 @@ open class SingleUserDefaultsStore<T: Codable> {
71
72
72
73
}
73
74
74
- // MARK: - Keys
75
+ // MARK: - Helpers
75
76
private extension SingleUserDefaultsStore {
76
77
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
+
77
86
/// store key for object.
78
87
var key : String {
79
88
return " \( uniqueIdentifier) -single-object "
You can’t perform that action at this time.
0 commit comments