diff --git a/Defaults.xcodeproj/project.pbxproj b/Defaults.xcodeproj/project.pbxproj index 124a801..7cd8367 100644 --- a/Defaults.xcodeproj/project.pbxproj +++ b/Defaults.xcodeproj/project.pbxproj @@ -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; diff --git a/Sources/Defaults/Defaults.swift b/Sources/Defaults/Defaults.swift index c8ec72b..f69cf72 100644 --- a/Sources/Defaults/Defaults.swift +++ b/Sources/Defaults/Defaults.swift @@ -45,7 +45,7 @@ public final class Defaults { fileprivate init() {} /// Access a defaults value using a `Defaults.Key`. - public static subscript(key: Defaults.Key) -> T { + public static subscript(key: Key) -> 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(key: Defaults.OptionalKey) -> T? { + public static subscript(key: OptionalKey) -> T? { get { key.suite[key] } set { key.suite[key] = newValue @@ -80,7 +80,7 @@ public final class Defaults { //=> false ``` */ - public static func reset(_ keys: Defaults.Key..., suite: UserDefaults = .standard) { + public static func reset(_ keys: Key..., suite: UserDefaults = .standard) { reset(keys, suite: suite) } @@ -104,7 +104,7 @@ public final class Defaults { //=> false ``` */ - public static func reset(_ keys: [Defaults.Key], suite: UserDefaults = .standard) { + public static func reset(_ keys: [Key], 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(_ keys: Defaults.OptionalKey..., suite: UserDefaults = .standard) { + public static func reset(_ keys: OptionalKey..., suite: UserDefaults = .standard) { reset(keys, suite: suite) } @@ -152,7 +152,7 @@ public final class Defaults { //=> nil ``` */ - public static func reset(_ keys: [Defaults.OptionalKey], suite: UserDefaults = .standard) { + public static func reset(_ keys: [OptionalKey], suite: UserDefaults = .standard) { for key in keys { key.suite[key] = nil } diff --git a/Tests/DefaultsTests/DefaultsTests.swift b/Tests/DefaultsTests/DefaultsTests.swift index 008861d..358f33d 100644 --- a/Tests/DefaultsTests/DefaultsTests.swift +++ b/Tests/DefaultsTests/DefaultsTests.swift @@ -56,7 +56,7 @@ final class DefaultsTests: XCTestCase { _ = Defaults.Key(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(keyName2, default: keyName2) XCTAssertEqual(UserDefaults.standard.string(forKey: keyName2), keyName2) diff --git a/readme.md b/readme.md index 0fb2525..4c37e51 100644 --- a/readme.md +++ b/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("isUnicorn", default: true) -} - -UserDefaults.standard[.isUnicorn] -//=> true -``` - -### Shared UserDefaults - -```swift -let extensionDefaults = UserDefaults(suiteName: "com.unicorn.app")! - -extension Defaults.Keys { - static let isUnicorn = Key("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("isUnicorn", default: true) +} + +UserDefaults.standard[.isUnicorn] +//=> true +``` + +### Shared `UserDefaults` + +```swift +let extensionDefaults = UserDefaults(suiteName: "com.unicorn.app")! + +extension Defaults.Keys { + static let isUnicorn = Key("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 {