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
+30-31Lines changed: 30 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,40 +9,54 @@ A Swift library for serializing `Codable` types to and from `Any` and `UserDefau
9
9
10
10
## Usage
11
11
12
-
Keyed types are encoded to a `[String: Any]`
12
+
[`RawRepresentable`](https://developer.apple.com/documentation/swift/rawrepresentable) types are encoded to their raw value:
13
+
14
+
```swift
15
+
// "fish"
16
+
let any =tryKeyValueEncoder().encode(Food(rawValue: "fish"))
17
+
```
18
+
19
+
Collection types are encoded to `[Any]`:
20
+
21
+
```swift
22
+
// ["fish", "chips"]
23
+
let any =tryKeyValueEncoder().encode(["fish", "chips"])
24
+
```
25
+
26
+
Structs and classes are encoded to a `[String: Any]`
13
27
14
28
```swift
15
29
structUser: Codable {
16
30
var id: Int
17
31
var name: String
18
32
}
19
33
20
-
// Decode from [String: Any]
21
-
let user =tryKeyValueEncoder().decode(
22
-
User.self,
23
-
from: ["id":99, "name":"Herbert"]
24
-
)
25
-
26
-
// Encode to [String: Any]
27
-
let dict =tryKeyValueEncoder().encode(user)
34
+
// ["id": 1, "name": "Herbert"]
35
+
let any =tryKeyValueEncoder().encode(User(id: 1, name: "Herbert"))
28
36
```
29
37
30
-
RawRepresentable types are encoded to their raw value:
38
+
Decode values from `Any`:
31
39
32
40
```swift
33
-
// Encode to String
34
-
let string =tryKeyValueEncoder().encode(Food(rawValue: "fish"))
41
+
let food =tryKeyValuDecoder().decode(Food.self, from: "fish")
42
+
43
+
let meals =tryKeyValuDecoder().decode([String].self, from: ["fish", "chips"])
44
+
45
+
let user =tryKeyValuDecoder().decode(User.self, from: ["id":1, "name":"Herbert"])
35
46
```
36
47
37
-
Decode values from `Any`:
48
+
[`DecodingError`](https://developer.apple.com/documentation/swift/decodingerror) is thrown when decoding fails. [`Context`](https://developer.apple.com/documentation/swift/decodingerror/context) will include a keyPath to the failed property.
38
49
39
50
```swift
40
-
let user =tryKeyValuDecoder().decode(User.self, from: ["id":99, "name":"Herbert"])
51
+
// throws DecodingError.typeMismatch 'Expected String at SELF[1], found Int'
52
+
let meals =tryKeyValuDecoder().decode([String].self, from: ["fish", 1])
41
53
42
-
let food =tryKeyValuDecoder().decode(Food.self, from: "fish")
54
+
// throws DecodingError.valueNotFound 'Expected String at SELF[1].name, found nil'
55
+
let user =tryKeyValuDecoder().decode(User.self, from: [["id":1, "name":"Herbert"], ["id:"2])
43
56
```
57
+
44
58
## UserDefaults
45
-
Store and retrieve any `Codable`type within UserDefaults:
59
+
Encode and decode `Codable`types with UserDefaults:
let duration =try UserDefaults.standard.decode(Duration.self, forKey: "duration")
84
98
```
85
-
86
-
[`DecodingError`](https://developer.apple.com/documentation/swift/decodingerror) is thrown when decoding fails. [`Context`](https://developer.apple.com/documentation/swift/decodingerror/context) will include a keyPath to the failed property.
87
-
88
-
```swift
89
-
UserDefaults.standard.set(
90
-
[
91
-
["id":99, "name":"Herbert"],
92
-
["id":100]
93
-
],
94
-
forKey: "users"
95
-
)
96
-
97
-
// throws DecodingError.valueNotFound 'Expected String at SELF[1].name, found nil'
98
-
let users =try UserDefaults.standard.decode([User].self, forKey: "users")
0 commit comments