Skip to content

Commit

Permalink
Docs improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Sep 11, 2019
1 parent 27c9997 commit eeb8c73
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 41 deletions.
6 changes: 6 additions & 0 deletions Defaults.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand All @@ -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;
};
Expand Down Expand Up @@ -982,6 +986,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SDKROOT = appletvos;
TARGETED_DEVICE_FAMILY = 3;
TVOS_DEPLOYMENT_TARGET = 10.0;
};
name = Debug;
Expand All @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions Sources/Defaults/Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ 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
}
}

/// 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
Expand All @@ -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)
}

Expand All @@ -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
}
Expand All @@ -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)
}

Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/DefaultsTests/DefaultsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
68 changes: 34 additions & 34 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -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

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.
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 {
Expand Down

0 comments on commit eeb8c73

Please sign in to comment.