Skip to content

Commit 6b5da2b

Browse files
authored
Update README.md
1 parent 29c6ae1 commit 6b5da2b

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

README.md

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,54 @@ A Swift library for serializing `Codable` types to and from `Any` and `UserDefau
99

1010
## Usage
1111

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 = try KeyValueEncoder().encode(Food(rawValue: "fish"))
17+
```
18+
19+
Collection types are encoded to `[Any]`:
20+
21+
```swift
22+
// ["fish", "chips"]
23+
let any = try KeyValueEncoder().encode(["fish", "chips"])
24+
```
25+
26+
Structs and classes are encoded to a `[String: Any]`
1327

1428
```swift
1529
struct User: Codable {
1630
var id: Int
1731
var name: String
1832
}
1933

20-
// Decode from [String: Any]
21-
let user = try KeyValueEncoder().decode(
22-
User.self,
23-
from: ["id": 99, "name": "Herbert"]
24-
)
25-
26-
// Encode to [String: Any]
27-
let dict = try KeyValueEncoder().encode(user)
34+
// ["id": 1, "name": "Herbert"]
35+
let any = try KeyValueEncoder().encode(User(id: 1, name: "Herbert"))
2836
```
2937

30-
RawRepresentable types are encoded to their raw value:
38+
Decode values from `Any`:
3139

3240
```swift
33-
// Encode to String
34-
let string = try KeyValueEncoder().encode(Food(rawValue: "fish"))
41+
let food = try KeyValuDecoder().decode(Food.self, from: "fish")
42+
43+
let meals = try KeyValuDecoder().decode([String].self, from: ["fish", "chips"])
44+
45+
let user = try KeyValuDecoder().decode(User.self, from: ["id": 1, "name": "Herbert"])
3546
```
3647

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.
3849

3950
```swift
40-
let user = try KeyValuDecoder().decode(User.self, from: ["id": 99, "name": "Herbert"])
51+
// throws DecodingError.typeMismatch 'Expected String at SELF[1], found Int'
52+
let meals = try KeyValuDecoder().decode([String].self, from: ["fish", 1])
4153

42-
let food = try KeyValuDecoder().decode(Food.self, from: "fish")
54+
// throws DecodingError.valueNotFound 'Expected String at SELF[1].name, found nil'
55+
let user = try KeyValuDecoder().decode(User.self, from: [["id": 1, "name": "Herbert"], ["id:" 2])
4356
```
57+
4458
## UserDefaults
45-
Store and retrieve any `Codable` type within UserDefaults:
59+
Encode and decode `Codable` types with UserDefaults:
4660

4761
```swift
4862
try UserDefaults.standard.encode(
@@ -82,18 +96,3 @@ let url = try UserDefaults.standard.decode(URL.self, forKey: "url")
8296

8397
let duration = try UserDefaults.standard.decode(Duration.self, forKey: "duration")
8498
```
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")
99-
```

0 commit comments

Comments
 (0)