You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+47-43Lines changed: 47 additions & 43 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,6 +8,51 @@ A Swift library for serializing `Codable` types to and from `Any` and `UserDefau
8
8
9
9
## Usage
10
10
11
+
### UserDefaults
12
+
13
+
Encode and decode [`Codable`](https://developer.apple.com/documentation/swift/codable) types with [`UserDefaults`](https://developer.apple.com/documentation/foundation/userdefaults):
14
+
15
+
```swift
16
+
try UserDefaults.standard.encode(
17
+
User(id: "1", name: "Herbert"),
18
+
forKey: "owner"
19
+
)
20
+
21
+
try UserDefaults.standard.encode(
22
+
URL(string: "fish.com"),
23
+
forKey: "url"
24
+
)
25
+
26
+
try UserDefaults.standard.encode(
27
+
Duration.nanoseconds(1),
28
+
forKey: "duration"
29
+
)
30
+
```
31
+
32
+
Values are persisted in a friendly representation of plist native types:
33
+
34
+
```swift
35
+
let defaults = UserDefaults.standard.dictionaryRepresentation()
36
+
37
+
[
38
+
"owner": ["id":1, "name":"Herbert"],
39
+
"url":URL(string: "fish.com"),
40
+
"duration": [0, 1000000000]
41
+
]
42
+
```
43
+
44
+
Decode values from the defaults:
45
+
46
+
```swift
47
+
let owner =try UserDefaults.standard.decode(Person.self, forKey: "owner")
48
+
49
+
let url =try UserDefaults.standard.decode(URL.self, forKey: "url")
50
+
51
+
let duration =try UserDefaults.standard.decode(Duration.self, forKey: "duration")
52
+
```
53
+
54
+
### KeyValueEncoder
55
+
11
56
[`RawRepresentable`](https://developer.apple.com/documentation/swift/rawrepresentable) types are encoded to their raw value:
12
57
13
58
```swift
@@ -34,6 +79,8 @@ struct User: Codable {
34
79
let any =tryKeyValueEncoder().encode(User(id: 1, name: "Herbert"))
35
80
```
36
81
82
+
### KeyValueDecoder
83
+
37
84
Decode values from `Any`:
38
85
39
86
```swift
@@ -57,7 +104,6 @@ let user = try KeyValueDecoder().decode(User.self, from: [["id": 1, "name": "Her
57
104
let ascii =tryKeyValueDecoder().decode([UInt8].self, from: [10, 100, 1000])
58
105
```
59
106
60
-
61
107
## Date Encoding/Decoding Strategy
62
108
63
109
The encoding of `Date` can be adjusted by setting the strategy.
let person = try decoder.decode(Person.self, from: dict)
180
226
```
181
-
182
-
## UserDefaults
183
-
Encode and decode [`Codable`](https://developer.apple.com/documentation/swift/codable) types with [`UserDefaults`](https://developer.apple.com/documentation/foundation/userdefaults):
184
-
185
-
```swift
186
-
try UserDefaults.standard.encode(
187
-
User(id: "1", name: "Herbert"),
188
-
forKey: "owner"
189
-
)
190
-
191
-
try UserDefaults.standard.encode(
192
-
URL(string: "fish.com"),
193
-
forKey: "url"
194
-
)
195
-
196
-
try UserDefaults.standard.encode(
197
-
Duration.nanoseconds(1),
198
-
forKey: "duration"
199
-
)
200
-
```
201
-
202
-
Values are persisted in a friendly representation of plist native types:
203
-
204
-
```swift
205
-
let defaults = UserDefaults.standard.dictionaryRepresentation()
206
-
207
-
[
208
-
"owner": ["id": 1, "name": "Herbert"],
209
-
"url": URL(string: "fish.com"),
210
-
"duration": [0, 1000000000]
211
-
]
212
-
```
213
-
214
-
Decode values from the defaults:
215
-
216
-
```swift
217
-
let owner = try UserDefaults.standard.decode(Person.self, forKey: "owner")
218
-
219
-
let url = try UserDefaults.standard.decode(URL.self, forKey: "url")
220
-
221
-
let duration = try UserDefaults.standard.decode(Duration.self, forKey: "duration")
0 commit comments