From ed390e9ac74a94e50f85a95fbfc7c3b587b5d106 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Thu, 21 May 2020 13:04:15 +0800 Subject: [PATCH] Minor tweaks --- Sources/Defaults/Defaults.swift | 14 +- Sources/Defaults/Observation+Combine.swift | 6 +- Sources/Defaults/Observation.swift | 6 +- Sources/Defaults/Reset.swift | 330 ++++++++++----------- Sources/Defaults/SwiftUI.swift | 2 + Sources/Defaults/UserDefaults.swift | 19 +- readme.md | 4 + 7 files changed, 201 insertions(+), 180 deletions(-) diff --git a/Sources/Defaults/Defaults.swift b/Sources/Defaults/Defaults.swift index 03bc06a..6d3637b 100644 --- a/Sources/Defaults/Defaults.swift +++ b/Sources/Defaults/Defaults.swift @@ -32,6 +32,7 @@ public enum Defaults { public let suite: UserDefaults /// Create a defaults key. + /// The `default` parameter can be left out if the `Value` type is an optional. public init(_ key: String, default defaultValue: Value, suite: UserDefaults = .standard) { self.name = key self.defaultValue = defaultValue @@ -59,6 +60,7 @@ public enum Defaults { public let suite: UserDefaults /// Create a defaults key. + /// The `default` parameter can be left out if the `Value` type is an optional. public init(_ key: String, default defaultValue: Value, suite: UserDefaults = .standard) { self.name = key self.defaultValue = defaultValue @@ -92,7 +94,7 @@ public enum Defaults { } /// Access a defaults value using a `Defaults.Key`. - public static subscript(key: Key) -> Value { + public static subscript(key: Key) -> Value { get { key.suite[key] } set { key.suite[key] = newValue @@ -101,7 +103,7 @@ public enum Defaults { /// Access a defaults value using a `Defaults.NSSecureCodingKey`. @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) - public static subscript(key: NSSecureCodingKey) -> Value { + public static subscript(key: NSSecureCodingKey) -> Value { get { key.suite[key] } set { key.suite[key] = newValue @@ -110,7 +112,7 @@ public enum Defaults { /// Access a defaults value using a `Defaults.NSSecureCodingOptionalKey`. @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) - public static subscript(key: NSSecureCodingOptionalKey) -> Value? { + public static subscript(key: NSSecureCodingOptionalKey) -> Value? { get { key.suite[key] } set { key.suite[key] = newValue @@ -121,11 +123,11 @@ public enum Defaults { extension Defaults { /** Remove all entries from the given `UserDefaults` suite. + + - Note: This only removes user-defined entries. System-defined entries will remain. */ public static func removeAll(suite: UserDefaults = .standard) { - for key in suite.dictionaryRepresentation().keys { - suite.removeObject(forKey: key) - } + suite.removeAll() } } diff --git a/Sources/Defaults/Observation+Combine.swift b/Sources/Defaults/Observation+Combine.swift index 5817431..a82ed65 100644 --- a/Sources/Defaults/Observation+Combine.swift +++ b/Sources/Defaults/Observation+Combine.swift @@ -90,7 +90,7 @@ extension Defaults { ``` */ @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *) - public static func publisher( + public static func publisher( _ key: Key, options: ObservationOptions = [.initial] ) -> AnyPublisher, Never> { @@ -104,7 +104,7 @@ extension Defaults { Returns a type-erased `Publisher` that publishes changes related to the given key. */ @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *) - public static func publisher( + public static func publisher( _ key: NSSecureCodingKey, options: ObservationOptions = [.initial] ) -> AnyPublisher, Never> { @@ -118,7 +118,7 @@ extension Defaults { Returns a type-erased `Publisher` that publishes changes related to the given optional key. */ @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *) - public static func publisher( + public static func publisher( _ key: NSSecureCodingOptionalKey, options: ObservationOptions = [.initial] ) -> AnyPublisher, Never> { diff --git a/Sources/Defaults/Observation.swift b/Sources/Defaults/Observation.swift index 5abd144..3a28618 100644 --- a/Sources/Defaults/Observation.swift +++ b/Sources/Defaults/Observation.swift @@ -219,7 +219,7 @@ extension Defaults { } ``` */ - public static func observe( + public static func observe( _ key: Key, options: ObservationOptions = [.initial], handler: @escaping (KeyChange) -> Void @@ -237,7 +237,7 @@ extension Defaults { Observe a defaults key. */ @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) - public static func observe( + public static func observe( _ key: NSSecureCodingKey, options: ObservationOptions = [.initial], handler: @escaping (NSSecureCodingKeyChange) -> Void @@ -255,7 +255,7 @@ extension Defaults { Observe an optional defaults key. */ @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) - public static func observe( + public static func observe( _ key: NSSecureCodingOptionalKey, options: ObservationOptions = [.initial], handler: @escaping (NSSecureCodingOptionalKeyChange) -> Void diff --git a/Sources/Defaults/Reset.swift b/Sources/Defaults/Reset.swift index b0f3090..e467c7f 100644 --- a/Sources/Defaults/Reset.swift +++ b/Sources/Defaults/Reset.swift @@ -95,7 +95,7 @@ extension Defaults { ``` */ public static func reset< - Value1: Codable + Value1 >( _ key1: Key ) { @@ -124,8 +124,8 @@ extension Defaults { ``` */ public static func reset< - Value1: Codable, - Value2: Codable + Value1, + Value2 >( _ key1: Key, _ key2: Key @@ -156,9 +156,9 @@ extension Defaults { ``` */ public static func reset< - Value1: Codable, - Value2: Codable, - Value3: Codable + Value1, + Value2, + Value3 >( _ key1: Key, _ key2: Key, @@ -191,10 +191,10 @@ extension Defaults { ``` */ public static func reset< - Value1: Codable, - Value2: Codable, - Value3: Codable, - Value4: Codable + Value1, + Value2, + Value3, + Value4 >( _ key1: Key, _ key2: Key, @@ -229,11 +229,11 @@ extension Defaults { ``` */ public static func reset< - Value1: Codable, - Value2: Codable, - Value3: Codable, - Value4: Codable, - Value5: Codable + Value1, + Value2, + Value3, + Value4, + Value5 >( _ key1: Key, _ key2: Key, @@ -270,12 +270,12 @@ extension Defaults { ``` */ public static func reset< - Value1: Codable, - Value2: Codable, - Value3: Codable, - Value4: Codable, - Value5: Codable, - Value6: Codable + Value1, + Value2, + Value3, + Value4, + Value5, + Value6 >( _ key1: Key, _ key2: Key, @@ -314,13 +314,13 @@ extension Defaults { ``` */ public static func reset< - Value1: Codable, - Value2: Codable, - Value3: Codable, - Value4: Codable, - Value5: Codable, - Value6: Codable, - Value7: Codable + Value1, + Value2, + Value3, + Value4, + Value5, + Value6, + Value7 >( _ key1: Key, _ key2: Key, @@ -361,14 +361,14 @@ extension Defaults { ``` */ public static func reset< - Value1: Codable, - Value2: Codable, - Value3: Codable, - Value4: Codable, - Value5: Codable, - Value6: Codable, - Value7: Codable, - Value8: Codable + Value1, + Value2, + Value3, + Value4, + Value5, + Value6, + Value7, + Value8 >( _ key1: Key, _ key2: Key, @@ -411,15 +411,15 @@ extension Defaults { ``` */ public static func reset< - Value1: Codable, - Value2: Codable, - Value3: Codable, - Value4: Codable, - Value5: Codable, - Value6: Codable, - Value7: Codable, - Value8: Codable, - Value9: Codable + Value1, + Value2, + Value3, + Value4, + Value5, + Value6, + Value7, + Value8, + Value9 >( _ key1: Key, _ key2: Key, @@ -464,16 +464,16 @@ extension Defaults { ``` */ public static func reset< - Value1: Codable, - Value2: Codable, - Value3: Codable, - Value4: Codable, - Value5: Codable, - Value6: Codable, - Value7: Codable, - Value8: Codable, - Value9: Codable, - Value10: Codable + Value1, + Value2, + Value3, + Value4, + Value5, + Value6, + Value7, + Value8, + Value9, + Value10 >( _ key1: Key, _ key2: Key, @@ -509,7 +509,7 @@ extension Defaults { */ @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) public static func reset< - Value1: NSSecureCoding + Value1 >( _ key1: NSSecureCodingKey ) { @@ -525,8 +525,8 @@ extension Defaults { */ @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) public static func reset< - Value1: NSSecureCoding, - Value2: NSSecureCoding + Value1, + Value2 >( _ key1: NSSecureCodingKey, _ key2: NSSecureCodingKey @@ -544,9 +544,9 @@ extension Defaults { */ @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) public static func reset< - Value1: NSSecureCoding, - Value2: NSSecureCoding, - Value3: NSSecureCoding + Value1, + Value2, + Value3 >( _ key1: NSSecureCodingKey, _ key2: NSSecureCodingKey, @@ -566,10 +566,10 @@ extension Defaults { */ @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) public static func reset< - Value1: NSSecureCoding, - Value2: NSSecureCoding, - Value3: NSSecureCoding, - Value4: NSSecureCoding + Value1, + Value2, + Value3, + Value4 >( _ key1: NSSecureCodingKey, _ key2: NSSecureCodingKey, @@ -591,11 +591,11 @@ extension Defaults { */ @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) public static func reset< - Value1: NSSecureCoding, - Value2: NSSecureCoding, - Value3: NSSecureCoding, - Value4: NSSecureCoding, - Value5: NSSecureCoding + Value1, + Value2, + Value3, + Value4, + Value5 >( _ key1: NSSecureCodingKey, _ key2: NSSecureCodingKey, @@ -619,12 +619,12 @@ extension Defaults { */ @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) public static func reset< - Value1: NSSecureCoding, - Value2: NSSecureCoding, - Value3: NSSecureCoding, - Value4: NSSecureCoding, - Value5: NSSecureCoding, - Value6: NSSecureCoding + Value1, + Value2, + Value3, + Value4, + Value5, + Value6 >( _ key1: NSSecureCodingKey, _ key2: NSSecureCodingKey, @@ -650,13 +650,13 @@ extension Defaults { */ @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) public static func reset< - Value1: NSSecureCoding, - Value2: NSSecureCoding, - Value3: NSSecureCoding, - Value4: NSSecureCoding, - Value5: NSSecureCoding, - Value6: NSSecureCoding, - Value7: NSSecureCoding + Value1, + Value2, + Value3, + Value4, + Value5, + Value6, + Value7 >( _ key1: NSSecureCodingKey, _ key2: NSSecureCodingKey, @@ -684,14 +684,14 @@ extension Defaults { */ @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) public static func reset< - Value1: NSSecureCoding, - Value2: NSSecureCoding, - Value3: NSSecureCoding, - Value4: NSSecureCoding, - Value5: NSSecureCoding, - Value6: NSSecureCoding, - Value7: NSSecureCoding, - Value8: NSSecureCoding + Value1, + Value2, + Value3, + Value4, + Value5, + Value6, + Value7, + Value8 >( _ key1: NSSecureCodingKey, _ key2: NSSecureCodingKey, @@ -721,15 +721,15 @@ extension Defaults { */ @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) public static func reset< - Value1: NSSecureCoding, - Value2: NSSecureCoding, - Value3: NSSecureCoding, - Value4: NSSecureCoding, - Value5: NSSecureCoding, - Value6: NSSecureCoding, - Value7: NSSecureCoding, - Value8: NSSecureCoding, - Value9: NSSecureCoding + Value1, + Value2, + Value3, + Value4, + Value5, + Value6, + Value7, + Value8, + Value9 >( _ key1: NSSecureCodingKey, _ key2: NSSecureCodingKey, @@ -761,16 +761,16 @@ extension Defaults { */ @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) public static func reset< - Value1: NSSecureCoding, - Value2: NSSecureCoding, - Value3: NSSecureCoding, - Value4: NSSecureCoding, - Value5: NSSecureCoding, - Value6: NSSecureCoding, - Value7: NSSecureCoding, - Value8: NSSecureCoding, - Value9: NSSecureCoding, - Value10: NSSecureCoding + Value1, + Value2, + Value3, + Value4, + Value5, + Value6, + Value7, + Value8, + Value9, + Value10 >( _ key1: NSSecureCodingKey, _ key2: NSSecureCodingKey, @@ -806,7 +806,7 @@ extension Defaults { */ @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) public static func reset< - Value1: NSSecureCoding + Value1 >( _ key1: NSSecureCodingOptionalKey ) { @@ -822,8 +822,8 @@ extension Defaults { */ @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) public static func reset< - Value1: NSSecureCoding, - Value2: NSSecureCoding + Value1, + Value2 >( _ key1: NSSecureCodingOptionalKey, _ key2: NSSecureCodingOptionalKey @@ -841,9 +841,9 @@ extension Defaults { */ @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) public static func reset< - Value1: NSSecureCoding, - Value2: NSSecureCoding, - Value3: NSSecureCoding + Value1, + Value2, + Value3 >( _ key1: NSSecureCodingOptionalKey, _ key2: NSSecureCodingOptionalKey, @@ -863,10 +863,10 @@ extension Defaults { */ @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) public static func reset< - Value1: NSSecureCoding, - Value2: NSSecureCoding, - Value3: NSSecureCoding, - Value4: NSSecureCoding + Value1, + Value2, + Value3, + Value4 >( _ key1: NSSecureCodingOptionalKey, _ key2: NSSecureCodingOptionalKey, @@ -888,11 +888,11 @@ extension Defaults { */ @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) public static func reset< - Value1: NSSecureCoding, - Value2: NSSecureCoding, - Value3: NSSecureCoding, - Value4: NSSecureCoding, - Value5: NSSecureCoding + Value1, + Value2, + Value3, + Value4, + Value5 >( _ key1: NSSecureCodingOptionalKey, _ key2: NSSecureCodingOptionalKey, @@ -916,12 +916,12 @@ extension Defaults { */ @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) public static func reset< - Value1: NSSecureCoding, - Value2: NSSecureCoding, - Value3: NSSecureCoding, - Value4: NSSecureCoding, - Value5: NSSecureCoding, - Value6: NSSecureCoding + Value1, + Value2, + Value3, + Value4, + Value5, + Value6 >( _ key1: NSSecureCodingOptionalKey, _ key2: NSSecureCodingOptionalKey, @@ -947,13 +947,13 @@ extension Defaults { */ @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) public static func reset< - Value1: NSSecureCoding, - Value2: NSSecureCoding, - Value3: NSSecureCoding, - Value4: NSSecureCoding, - Value5: NSSecureCoding, - Value6: NSSecureCoding, - Value7: NSSecureCoding + Value1, + Value2, + Value3, + Value4, + Value5, + Value6, + Value7 >( _ key1: NSSecureCodingOptionalKey, _ key2: NSSecureCodingOptionalKey, @@ -981,14 +981,14 @@ extension Defaults { */ @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) public static func reset< - Value1: NSSecureCoding, - Value2: NSSecureCoding, - Value3: NSSecureCoding, - Value4: NSSecureCoding, - Value5: NSSecureCoding, - Value6: NSSecureCoding, - Value7: NSSecureCoding, - Value8: NSSecureCoding + Value1, + Value2, + Value3, + Value4, + Value5, + Value6, + Value7, + Value8 >( _ key1: NSSecureCodingOptionalKey, _ key2: NSSecureCodingOptionalKey, @@ -1018,15 +1018,15 @@ extension Defaults { */ @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) public static func reset< - Value1: NSSecureCoding, - Value2: NSSecureCoding, - Value3: NSSecureCoding, - Value4: NSSecureCoding, - Value5: NSSecureCoding, - Value6: NSSecureCoding, - Value7: NSSecureCoding, - Value8: NSSecureCoding, - Value9: NSSecureCoding + Value1, + Value2, + Value3, + Value4, + Value5, + Value6, + Value7, + Value8, + Value9 >( _ key1: NSSecureCodingOptionalKey, _ key2: NSSecureCodingOptionalKey, @@ -1058,16 +1058,16 @@ extension Defaults { */ @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) public static func reset< - Value1: NSSecureCoding, - Value2: NSSecureCoding, - Value3: NSSecureCoding, - Value4: NSSecureCoding, - Value5: NSSecureCoding, - Value6: NSSecureCoding, - Value7: NSSecureCoding, - Value8: NSSecureCoding, - Value9: NSSecureCoding, - Value10: NSSecureCoding + Value1, + Value2, + Value3, + Value4, + Value5, + Value6, + Value7, + Value8, + Value9, + Value10 >( _ key1: NSSecureCodingOptionalKey, _ key2: NSSecureCodingOptionalKey, diff --git a/Sources/Defaults/SwiftUI.swift b/Sources/Defaults/SwiftUI.swift index a32dfaa..1a83a5d 100644 --- a/Sources/Defaults/SwiftUI.swift +++ b/Sources/Defaults/SwiftUI.swift @@ -50,6 +50,8 @@ public struct Default: DynamicProperty { /** Get/set a `Defaults` item and also have the view be updated when the value changes. This is similar to `@State`. + - Important: You cannot use this in an `ObservableObject`. It's meant to be used in a `View`. + ``` extension Defaults.Keys { static let hasUnicorn = Key("hasUnicorn", default: false) diff --git a/Sources/Defaults/UserDefaults.swift b/Sources/Defaults/UserDefaults.swift index e9d70d1..147c928 100644 --- a/Sources/Defaults/UserDefaults.swift +++ b/Sources/Defaults/UserDefaults.swift @@ -81,7 +81,7 @@ extension UserDefaults { set(try? NSKeyedArchiver.archivedData(withRootObject: value, requiringSecureCoding: true), forKey: key) } - public subscript(key: Defaults.Key) -> Value { + public subscript(key: Defaults.Key) -> Value { get { _get(key.name) ?? key.defaultValue } set { _set(key.name, to: newValue) @@ -89,7 +89,7 @@ extension UserDefaults { } @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) - public subscript(key: Defaults.NSSecureCodingKey) -> Value { + public subscript(key: Defaults.NSSecureCodingKey) -> Value { get { _get(key.name) ?? key.defaultValue } set { _set(key.name, to: newValue) @@ -97,7 +97,7 @@ extension UserDefaults { } @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) - public subscript(key: Defaults.NSSecureCodingOptionalKey) -> Value? { + public subscript(key: Defaults.NSSecureCodingOptionalKey) -> Value? { get { _get(key.name) } set { guard let value = newValue else { @@ -132,3 +132,16 @@ extension UserDefaults { } } } + +extension UserDefaults { + /** + Remove all entries. + + - Note: This only removes user-defined entries. System-defined entries will remain. + */ + public func removeAll() { + for key in dictionaryRepresentation().keys { + removeObject(forKey: key) + } + } +} diff --git a/readme.md b/readme.md index 9dbd671..826196a 100644 --- a/readme.md +++ b/readme.md @@ -8,6 +8,8 @@ It uses `NSUserDefaults` underneath but exposes a type-safe facade with lots of It's 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). +For a real-world example, see my [Plash app](https://github.com/sindresorhus/Plash/blob/533dbc888d8ba3bd9581e60320af282a22c53f85/Plash/Constants.swift#L9-L18). + ## Highlights - **Strongly typed:** You declare the type and default value upfront. @@ -161,6 +163,8 @@ struct ContentView: View { Note that it's `@Default`, not `@Defaults`. +You cannot use `@Default` in an `ObservableObject`. It's meant to be used in a `View`. + This is only implemented for `Defaults.Key`. PR welcome for `Defaults.NSSecureCoding` if you need it. ### Observe changes to a key