Minor documentation tweaks

This commit is contained in:
Sindre Sorhus 2021-07-12 14:24:20 +02:00
parent 1b629298f4
commit 012a475cc0
2 changed files with 35 additions and 26 deletions

View File

@ -3,18 +3,22 @@ import Foundation
extension Defaults { extension Defaults {
/** /**
Type-erased wrappers for `Defaults.Serializable` values. Type-erased wrapper for `Defaults.Serializable` values.
It can be used when the user wants to create an `Any` value that conforms to `Defaults.Serializable`.
It will have an internal property `value` which` should always be a UserDefaults natively supported type.
`get` will deserialize internal value to the type that user explicit in the function parameter. It can be useful when you need to create an `Any` value that conforms to `Defaults.Serializable`.
It will have an internal property `value` which should always be a `UserDefaults` natively supported type.
`get` will deserialize the internal value to the type that user specify in the function parameter.
``` ```
let any = Defaults.Key<Defaults.AnySerializable>("independentAnyKey", default: 121_314) let any = Defaults.Key<Defaults.AnySerializable>("independentAnyKey", default: 121_314)
print(Defaults[any].get(Int.self)) //=> 121_314
print(Defaults[any].get(Int.self))
//=> 121_314
``` ```
- Note: the only way to assign a non-serializable value is using `ExpressibleByArrayLiteral` or `ExpressibleByDictionaryLiteral` to assign a type that is not UserDefaults natively supported type. - Note: The only way to assign a non-serializable value is using `ExpressibleByArrayLiteral` or `ExpressibleByDictionaryLiteral` to assign a type that is not a `UserDefaults` natively supported type.
``` ```
private enum mime: String, Defaults.Serializable { private enum mime: String, Defaults.Serializable {
@ -25,7 +29,7 @@ extension Defaults {
let any = Defaults.Key<Defaults.AnySerializable>("independentAnyKey", default: [mime.JSON]) let any = Defaults.Key<Defaults.AnySerializable>("independentAnyKey", default: [mime.JSON])
``` ```
*/ */
public struct AnySerializable: Defaults.Serializable { public struct AnySerializable: Serializable {
var value: Any var value: Any
public static let bridge = AnyBridge() public static let bridge = AnyBridge()
@ -99,7 +103,7 @@ extension Defaults.AnySerializable: Hashable {
} }
extension Defaults.AnySerializable: Equatable { extension Defaults.AnySerializable: Equatable {
public static func == (lhs: Defaults.AnySerializable, rhs: Defaults.AnySerializable) -> Bool { public static func == (lhs: Self, rhs: Self) -> Bool {
switch (lhs.value, rhs.value) { switch (lhs.value, rhs.value) {
case let (lhs as Data, rhs as Data): case let (lhs as Data, rhs as Data):
return lhs == rhs return lhs == rhs
@ -188,7 +192,7 @@ extension Defaults.AnySerializable: ExpressibleByDictionaryLiteral {
} }
extension Defaults.AnySerializable: _DefaultsOptionalType { extension Defaults.AnySerializable: _DefaultsOptionalType {
/// Since nil cannot assign to `Any`, we use `Void` instead of `nil`. // Since `nil` cannot be assigned to `Any`, we use `Void` instead of `nil`.
public var isNil: Bool { value is Void } public var isNil: Bool { value is Void }
} }

View File

@ -434,11 +434,11 @@ Defaults.AnySerializable<Value: Defaults.Serializable>(_ value: Value)
Type: `class` Type: `class`
Type-erased wrappers for `Defaults.Serializable` values. Type-erased wrapper for `Defaults.Serializable` values.
- `get<Value: Defaults.Serializable>() -> Value?`: Retrieve the value which type is `Value` from the UserDefaults. - `get<Value: Defaults.Serializable>() -> Value?`: Retrieve the value which type is `Value` from UserDefaults.
- `get<Value: Defaults.Serializable>(_: Value.Type) -> Value?`: Specific the `Value` that you want to retrieve, this will be useful in some ambiguous cases. - `get<Value: Defaults.Serializable>(_: Value.Type) -> Value?`: Specify the `Value` you want to retrieve. This can be useful in some ambiguous cases.
- `set<Value: Defaults.Serializable>(_ newValue: Value)`: Set newValue into `Defaults.AnySerializable`. - `set<Value: Defaults.Serializable>(_ newValue: Value)`: Set a new value for `Defaults.AnySerializable`.
#### `Defaults.reset(keys…)` #### `Defaults.reset(keys…)`
@ -678,18 +678,17 @@ Defaults[.dictionaryUser]["user"]?.name //=> "Hello"
### Dynamic value ### Dynamic value
There might be situations where you want to use `[String: Any]` directly. There might be situations where you want to use `[String: Any]` directly, but `Defaults` need its values to conform to `Defaults.Serializable`. The type-eraser `Defaults.AnySerializable` helps overcome this limitation.
But `Defaults` need its value to conform to `Defaults.Serializable`, so here is a class `Defaults.AnySerializable` to overcome this limitation.
`Defaults.AnySerializable` only available for `Value` which conforms to `Defaults.Serializable`. `Defaults.AnySerializable` is only available for values that conform to `Defaults.Serializable`.
Warn: The type erasure should only be used when there's no other way to handle it because it has much worse performance. It should only be used in wrapped types. For example, wrapped in `Array`, `Set` or `Dictionary`. Warning: The type-eraser should only be used when there's no other way to handle it because it has much worse performance. It should only be used in wrapped types. For example, wrapped in `Array`, `Set` or `Dictionary`.
#### Primitive type #### Primitive type
`Defaults.AnySerializable` conforms to `ExpressibleByStringLiteral`, `ExpressibleByIntegerLiteral`, `ExpressibleByFloatLiteral`, `ExpressibleByBooleanLiteral`, `ExpressibleByNilLiteral`, `ExpressibleByArrayLiteral` and `ExpressibleByDictionaryLiteral`. `Defaults.AnySerializable` conforms to `ExpressibleByStringLiteral`, `ExpressibleByIntegerLiteral`, `ExpressibleByFloatLiteral`, `ExpressibleByBooleanLiteral`, `ExpressibleByNilLiteral`, `ExpressibleByArrayLiteral`, and `ExpressibleByDictionaryLiteral`.
So it can assign directly with these primitive types. Which means you can assign these primitive types directly:
```swift ```swift
let any = Defaults.Key<Defaults.AnySerializable>("anyKey", default: 1) let any = Defaults.Key<Defaults.AnySerializable>("anyKey", default: 1)
@ -698,9 +697,9 @@ Defaults[any] = "🦄"
#### Other types #### Other types
##### Using `get`, `set` ##### Using `get` and `set`
For other types, you will have to assign it like this. For other types, you will have to assign it like this:
```swift ```swift
enum mime: String, Defaults.Serializable { enum mime: String, Defaults.Serializable {
@ -711,18 +710,23 @@ enum mime: String, Defaults.Serializable {
let any = Defaults.Key<Defaults.AnySerializable>("anyKey", default: [Defaults.AnySerializable(mime.JSON)]) let any = Defaults.Key<Defaults.AnySerializable>("anyKey", default: [Defaults.AnySerializable(mime.JSON)])
if let mimeType: mime = Defaults[any].get() { if let mimeType: mime = Defaults[any].get() {
print(mimeType.rawValue) //=> "application/json" print(mimeType.rawValue)
//=> "application/json"
} }
Defaults[any].set(mime.STREAM) Defaults[any].set(mime.STREAM)
if let mimeType: mime = Defaults[any].get() { if let mimeType: mime = Defaults[any].get() {
print(mimeType.rawValue) //=> "application/octet-stream" print(mimeType.rawValue)
//=> "application/octet-stream"
} }
``` ```
#### Wrapped in `Array`, `Set`, `Dictionary` #### Wrapped in `Array`, `Set`, or `Dictionary`
`Defaults.AnySerializable` also support the above types wrapped in `Array`, `Set`, `Dictionary` `Defaults.AnySerializable` also support the above types wrapped in `Array`, `Set`, `Dictionary`.
Here is the example for `[String: Defaults.AnySerializable]`.
Here is the example for `[String: Defaults.AnySerializable]`:
```swift ```swift
extension Defaults.Keys { extension Defaults.Keys {
@ -744,6 +748,7 @@ if let value: String = Defaults[.magic]["unicorn"]?.get() {
Defaults[.magic]["number"] = 3 Defaults[.magic]["number"] = 3
Defaults[.magic]["boolean"] = true Defaults[.magic]["boolean"] = true
Defaults[.magic]["enum"] = Defaults.AnySerializable(mime.JSON) Defaults[.magic]["enum"] = Defaults.AnySerializable(mime.JSON)
if let mimeType: mime = Defaults[.magic]["enum"]?.get() { if let mimeType: mime = Defaults[.magic]["enum"]?.get() {
print(mimeType.rawValue) print(mimeType.rawValue)
//=> "application/json" //=> "application/json"