Document how to store a dictionary of arbitrary values

Fixes #36
This commit is contained in:
Sindre Sorhus 2020-04-13 13:39:25 +08:00
parent 8376ca7f51
commit 77c05abe10
1 changed files with 24 additions and 0 deletions

View File

@ -489,6 +489,30 @@ The effects of any call to `tieToLifetime(of:)` are reversed. Note however that
## FAQ
### How can I store a dictionary of arbitrary values?
You cannot store `[String: Any]` directly as it cannot conform to `Codable`. However, you can use the [`AnyCodable`](https://github.com/Flight-School/AnyCodable) package to work around this `Codable` limitation:
```swift
import AnyCodable
extension Defaults.Keys {
static let magic = Key<[String: AnyCodable]>("magic", default: [:])
}
// …
Defaults[.magic]["unicorn"] = "🦄"
if let value = Defaults[.magic]["unicorn"]?.value {
print(value)
//=> "🦄"
}
Defaults[.magic]["number"] = 3
Defaults[.magic]["boolean"] = true
```
### How is this different from [`SwiftyUserDefaults`](https://github.com/radex/SwiftyUserDefaults)?
It's inspired by that package and other solutions. The main difference is that this module doesn't hardcode the default values and comes with Codable support.