Docs improvements
This commit is contained in:
parent
27c9997134
commit
eeb8c73d7f
|
@ -702,8 +702,10 @@
|
|||
PRODUCT_BUNDLE_IDENTIFIER = "com.Defaults.Defaults-iOS";
|
||||
PRODUCT_NAME = Defaults;
|
||||
SKIP_INSTALL = YES;
|
||||
SUPPORTS_MACCATALYST = NO;
|
||||
SWIFT_COMPILATION_MODE = singlefile;
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
|
||||
TARGETED_DEVICE_FAMILY = "1,2";
|
||||
};
|
||||
name = Debug;
|
||||
};
|
||||
|
@ -728,8 +730,10 @@
|
|||
PRODUCT_BUNDLE_IDENTIFIER = "com.Defaults.Defaults-iOS";
|
||||
PRODUCT_NAME = Defaults;
|
||||
SKIP_INSTALL = YES;
|
||||
SUPPORTS_MACCATALYST = NO;
|
||||
SWIFT_COMPILATION_MODE = wholemodule;
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-O";
|
||||
TARGETED_DEVICE_FAMILY = "1,2";
|
||||
};
|
||||
name = Release;
|
||||
};
|
||||
|
@ -982,6 +986,7 @@
|
|||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
PROVISIONING_PROFILE_SPECIFIER = "";
|
||||
SDKROOT = appletvos;
|
||||
TARGETED_DEVICE_FAMILY = 3;
|
||||
TVOS_DEPLOYMENT_TARGET = 10.0;
|
||||
};
|
||||
name = Debug;
|
||||
|
@ -1003,6 +1008,7 @@
|
|||
SDKROOT = appletvos;
|
||||
SWIFT_COMPILATION_MODE = wholemodule;
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-O";
|
||||
TARGETED_DEVICE_FAMILY = 3;
|
||||
TVOS_DEPLOYMENT_TARGET = 10.0;
|
||||
};
|
||||
name = Release;
|
||||
|
|
|
@ -45,7 +45,7 @@ public final class Defaults {
|
|||
fileprivate init() {}
|
||||
|
||||
/// Access a defaults value using a `Defaults.Key`.
|
||||
public static subscript<T: Codable>(key: Defaults.Key<T>) -> T {
|
||||
public static subscript<T: Codable>(key: Key<T>) -> T {
|
||||
get { key.suite[key] }
|
||||
set {
|
||||
key.suite[key] = newValue
|
||||
|
@ -53,7 +53,7 @@ public final class Defaults {
|
|||
}
|
||||
|
||||
/// Access a defaults value using a `Defaults.OptionalKey`.
|
||||
public static subscript<T: Codable>(key: Defaults.OptionalKey<T>) -> T? {
|
||||
public static subscript<T: Codable>(key: OptionalKey<T>) -> T? {
|
||||
get { key.suite[key] }
|
||||
set {
|
||||
key.suite[key] = newValue
|
||||
|
@ -80,7 +80,7 @@ public final class Defaults {
|
|||
//=> false
|
||||
```
|
||||
*/
|
||||
public static func reset<T: Codable>(_ keys: Defaults.Key<T>..., suite: UserDefaults = .standard) {
|
||||
public static func reset<T: Codable>(_ keys: Key<T>..., suite: UserDefaults = .standard) {
|
||||
reset(keys, suite: suite)
|
||||
}
|
||||
|
||||
|
@ -104,7 +104,7 @@ public final class Defaults {
|
|||
//=> false
|
||||
```
|
||||
*/
|
||||
public static func reset<T: Codable>(_ keys: [Defaults.Key<T>], suite: UserDefaults = .standard) {
|
||||
public static func reset<T: Codable>(_ keys: [Key<T>], suite: UserDefaults = .standard) {
|
||||
for key in keys {
|
||||
key.suite[key] = key.defaultValue
|
||||
}
|
||||
|
@ -129,7 +129,7 @@ public final class Defaults {
|
|||
//=> nil
|
||||
```
|
||||
*/
|
||||
public static func reset<T: Codable>(_ keys: Defaults.OptionalKey<T>..., suite: UserDefaults = .standard) {
|
||||
public static func reset<T: Codable>(_ keys: OptionalKey<T>..., suite: UserDefaults = .standard) {
|
||||
reset(keys, suite: suite)
|
||||
}
|
||||
|
||||
|
@ -152,7 +152,7 @@ public final class Defaults {
|
|||
//=> nil
|
||||
```
|
||||
*/
|
||||
public static func reset<T: Codable>(_ keys: [Defaults.OptionalKey<T>], suite: UserDefaults = .standard) {
|
||||
public static func reset<T: Codable>(_ keys: [OptionalKey<T>], suite: UserDefaults = .standard) {
|
||||
for key in keys {
|
||||
key.suite[key] = nil
|
||||
}
|
||||
|
|
|
@ -56,7 +56,7 @@ final class DefaultsTests: XCTestCase {
|
|||
_ = Defaults.Key<Bool>(keyName, default: true)
|
||||
XCTAssertEqual(UserDefaults.standard.bool(forKey: keyName), true)
|
||||
|
||||
// Test that it works with multiple keys with Defaults.
|
||||
// Test that it works with multiple keys with `Defaults`.
|
||||
let keyName2 = "registersDefault2"
|
||||
_ = Defaults.Key<String>(keyName2, default: keyName2)
|
||||
XCTAssertEqual(UserDefaults.standard.string(forKey: keyName2), keyName2)
|
||||
|
|
68
readme.md
68
readme.md
|
@ -2,7 +2,7 @@
|
|||
|
||||
> Swifty and modern [UserDefaults](https://developer.apple.com/documentation/foundation/userdefaults)
|
||||
|
||||
This package is used in production by the [Gifski](https://github.com/sindresorhus/Gifski), [Lungo](https://sindresorhus.com/lungo), [Battery Indicator](https://sindresorhus.com/battery-indicator), and [HEIC Converter](https://sindresorhus.com/heic-converter) app.
|
||||
This package is used in production by apps like [Gifski](https://github.com/sindresorhus/Gifski), [Dato](https://sindresorhus.com/dato), [Lungo](https://sindresorhus.com/lungo), [Battery Indicator](https://sindresorhus.com/battery-indicator), and [HEIC Converter](https://sindresorhus.com/heic-converter).
|
||||
|
||||
|
||||
## Highlights
|
||||
|
@ -105,37 +105,6 @@ Defaults[.defaultDuration].rawValue
|
|||
//=> "1 Hour"
|
||||
```
|
||||
|
||||
### It's just UserDefaults with sugar
|
||||
|
||||
This works too:
|
||||
|
||||
```swift
|
||||
extension Defaults.Keys {
|
||||
static let isUnicorn = Key<Bool>("isUnicorn", default: true)
|
||||
}
|
||||
|
||||
UserDefaults.standard[.isUnicorn]
|
||||
//=> true
|
||||
```
|
||||
|
||||
### Shared UserDefaults
|
||||
|
||||
```swift
|
||||
let extensionDefaults = UserDefaults(suiteName: "com.unicorn.app")!
|
||||
|
||||
extension Defaults.Keys {
|
||||
static let isUnicorn = Key<Bool>("isUnicorn", default: true, suite: extensionDefaults)
|
||||
}
|
||||
|
||||
Defaults[.isUnicorn]
|
||||
//=> true
|
||||
|
||||
// Or
|
||||
|
||||
extensionDefaults[.isUnicorn]
|
||||
//=> true
|
||||
```
|
||||
|
||||
### Use keys directly
|
||||
|
||||
You are not required to attach keys to `Defaults.Keys`.
|
||||
|
@ -191,9 +160,40 @@ Defaults[.isUnicornMode]
|
|||
|
||||
This works for `OptionalKey` too, which will be reset back to `nil`.
|
||||
|
||||
### Default values are registered with UserDefaults
|
||||
### It's just `UserDefaults` with sugar
|
||||
|
||||
When you create a `Defaults.Key`, it automatically registers the `default` value with normal UserDefaults. This means you can make use of the default value in, for example, bindings in Interface Builder.
|
||||
This works too:
|
||||
|
||||
```swift
|
||||
extension Defaults.Keys {
|
||||
static let isUnicorn = Key<Bool>("isUnicorn", default: true)
|
||||
}
|
||||
|
||||
UserDefaults.standard[.isUnicorn]
|
||||
//=> true
|
||||
```
|
||||
|
||||
### Shared `UserDefaults`
|
||||
|
||||
```swift
|
||||
let extensionDefaults = UserDefaults(suiteName: "com.unicorn.app")!
|
||||
|
||||
extension Defaults.Keys {
|
||||
static let isUnicorn = Key<Bool>("isUnicorn", default: true, suite: extensionDefaults)
|
||||
}
|
||||
|
||||
Defaults[.isUnicorn]
|
||||
//=> true
|
||||
|
||||
// Or
|
||||
|
||||
extensionDefaults[.isUnicorn]
|
||||
//=> true
|
||||
```
|
||||
|
||||
### Default values are registered with `UserDefaults`
|
||||
|
||||
When you create a `Defaults.Key`, it automatically registers the `default` value with normal `UserDefaults`. This means you can make use of the default value in, for example, bindings in Interface Builder.
|
||||
|
||||
```swift
|
||||
extension Defaults.Keys {
|
||||
|
|
Loading…
Reference in New Issue