Docs improvements

This commit is contained in:
Sindre Sorhus 2019-09-11 14:56:11 +07:00
parent 27c9997134
commit eeb8c73d7f
4 changed files with 47 additions and 41 deletions

View File

@ -702,8 +702,10 @@
PRODUCT_BUNDLE_IDENTIFIER = "com.Defaults.Defaults-iOS"; PRODUCT_BUNDLE_IDENTIFIER = "com.Defaults.Defaults-iOS";
PRODUCT_NAME = Defaults; PRODUCT_NAME = Defaults;
SKIP_INSTALL = YES; SKIP_INSTALL = YES;
SUPPORTS_MACCATALYST = NO;
SWIFT_COMPILATION_MODE = singlefile; SWIFT_COMPILATION_MODE = singlefile;
SWIFT_OPTIMIZATION_LEVEL = "-Onone"; SWIFT_OPTIMIZATION_LEVEL = "-Onone";
TARGETED_DEVICE_FAMILY = "1,2";
}; };
name = Debug; name = Debug;
}; };
@ -728,8 +730,10 @@
PRODUCT_BUNDLE_IDENTIFIER = "com.Defaults.Defaults-iOS"; PRODUCT_BUNDLE_IDENTIFIER = "com.Defaults.Defaults-iOS";
PRODUCT_NAME = Defaults; PRODUCT_NAME = Defaults;
SKIP_INSTALL = YES; SKIP_INSTALL = YES;
SUPPORTS_MACCATALYST = NO;
SWIFT_COMPILATION_MODE = wholemodule; SWIFT_COMPILATION_MODE = wholemodule;
SWIFT_OPTIMIZATION_LEVEL = "-O"; SWIFT_OPTIMIZATION_LEVEL = "-O";
TARGETED_DEVICE_FAMILY = "1,2";
}; };
name = Release; name = Release;
}; };
@ -982,6 +986,7 @@
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = ""; PROVISIONING_PROFILE_SPECIFIER = "";
SDKROOT = appletvos; SDKROOT = appletvos;
TARGETED_DEVICE_FAMILY = 3;
TVOS_DEPLOYMENT_TARGET = 10.0; TVOS_DEPLOYMENT_TARGET = 10.0;
}; };
name = Debug; name = Debug;
@ -1003,6 +1008,7 @@
SDKROOT = appletvos; SDKROOT = appletvos;
SWIFT_COMPILATION_MODE = wholemodule; SWIFT_COMPILATION_MODE = wholemodule;
SWIFT_OPTIMIZATION_LEVEL = "-O"; SWIFT_OPTIMIZATION_LEVEL = "-O";
TARGETED_DEVICE_FAMILY = 3;
TVOS_DEPLOYMENT_TARGET = 10.0; TVOS_DEPLOYMENT_TARGET = 10.0;
}; };
name = Release; name = Release;

View File

@ -45,7 +45,7 @@ public final class Defaults {
fileprivate init() {} fileprivate init() {}
/// Access a defaults value using a `Defaults.Key`. /// 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] } get { key.suite[key] }
set { set {
key.suite[key] = newValue key.suite[key] = newValue
@ -53,7 +53,7 @@ public final class Defaults {
} }
/// Access a defaults value using a `Defaults.OptionalKey`. /// 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] } get { key.suite[key] }
set { set {
key.suite[key] = newValue key.suite[key] = newValue
@ -80,7 +80,7 @@ public final class Defaults {
//=> false //=> 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) reset(keys, suite: suite)
} }
@ -104,7 +104,7 @@ public final class Defaults {
//=> false //=> 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 { for key in keys {
key.suite[key] = key.defaultValue key.suite[key] = key.defaultValue
} }
@ -129,7 +129,7 @@ public final class Defaults {
//=> nil //=> 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) reset(keys, suite: suite)
} }
@ -152,7 +152,7 @@ public final class Defaults {
//=> nil //=> 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 { for key in keys {
key.suite[key] = nil key.suite[key] = nil
} }

View File

@ -56,7 +56,7 @@ final class DefaultsTests: XCTestCase {
_ = Defaults.Key<Bool>(keyName, default: true) _ = Defaults.Key<Bool>(keyName, default: true)
XCTAssertEqual(UserDefaults.standard.bool(forKey: keyName), 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" let keyName2 = "registersDefault2"
_ = Defaults.Key<String>(keyName2, default: keyName2) _ = Defaults.Key<String>(keyName2, default: keyName2)
XCTAssertEqual(UserDefaults.standard.string(forKey: keyName2), keyName2) XCTAssertEqual(UserDefaults.standard.string(forKey: keyName2), keyName2)

View File

@ -2,7 +2,7 @@
> Swifty and modern [UserDefaults](https://developer.apple.com/documentation/foundation/userdefaults) > 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 ## Highlights
@ -105,37 +105,6 @@ Defaults[.defaultDuration].rawValue
//=> "1 Hour" //=> "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 ### Use keys directly
You are not required to attach keys to `Defaults.Keys`. 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`. 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 ```swift
extension Defaults.Keys { extension Defaults.Keys {