Minor tweaks

This commit is contained in:
Sindre Sorhus 2020-05-21 13:04:15 +08:00
parent f67098fd5c
commit ed390e9ac7
7 changed files with 201 additions and 180 deletions

View File

@ -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<Value: Codable>(key: Key<Value>) -> Value {
public static subscript<Value>(key: Key<Value>) -> 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<Value: NSSecureCoding>(key: NSSecureCodingKey<Value>) -> Value {
public static subscript<Value>(key: NSSecureCodingKey<Value>) -> 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<Value: NSSecureCoding>(key: NSSecureCodingOptionalKey<Value>) -> Value? {
public static subscript<Value>(key: NSSecureCodingOptionalKey<Value>) -> 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()
}
}

View File

@ -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<Value: Codable>(
public static func publisher<Value>(
_ key: Key<Value>,
options: ObservationOptions = [.initial]
) -> AnyPublisher<KeyChange<Value>, 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<Value: NSSecureCoding>(
public static func publisher<Value>(
_ key: NSSecureCodingKey<Value>,
options: ObservationOptions = [.initial]
) -> AnyPublisher<NSSecureCodingKeyChange<Value>, 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<Value: NSSecureCoding>(
public static func publisher<Value>(
_ key: NSSecureCodingOptionalKey<Value>,
options: ObservationOptions = [.initial]
) -> AnyPublisher<NSSecureCodingOptionalKeyChange<Value>, Never> {

View File

@ -219,7 +219,7 @@ extension Defaults {
}
```
*/
public static func observe<Value: Codable>(
public static func observe<Value>(
_ key: Key<Value>,
options: ObservationOptions = [.initial],
handler: @escaping (KeyChange<Value>) -> 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<Value: NSSecureCoding>(
public static func observe<Value>(
_ key: NSSecureCodingKey<Value>,
options: ObservationOptions = [.initial],
handler: @escaping (NSSecureCodingKeyChange<Value>) -> 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<Value: NSSecureCoding>(
public static func observe<Value>(
_ key: NSSecureCodingOptionalKey<Value>,
options: ObservationOptions = [.initial],
handler: @escaping (NSSecureCodingOptionalKeyChange<Value>) -> Void

View File

@ -95,7 +95,7 @@ extension Defaults {
```
*/
public static func reset<
Value1: Codable
Value1
>(
_ key1: Key<Value1>
) {
@ -124,8 +124,8 @@ extension Defaults {
```
*/
public static func reset<
Value1: Codable,
Value2: Codable
Value1,
Value2
>(
_ key1: Key<Value1>,
_ key2: Key<Value2>
@ -156,9 +156,9 @@ extension Defaults {
```
*/
public static func reset<
Value1: Codable,
Value2: Codable,
Value3: Codable
Value1,
Value2,
Value3
>(
_ key1: Key<Value1>,
_ key2: Key<Value2>,
@ -191,10 +191,10 @@ extension Defaults {
```
*/
public static func reset<
Value1: Codable,
Value2: Codable,
Value3: Codable,
Value4: Codable
Value1,
Value2,
Value3,
Value4
>(
_ key1: Key<Value1>,
_ key2: Key<Value2>,
@ -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<Value1>,
_ key2: Key<Value2>,
@ -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<Value1>,
_ key2: Key<Value2>,
@ -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<Value1>,
_ key2: Key<Value2>,
@ -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<Value1>,
_ key2: Key<Value2>,
@ -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<Value1>,
_ key2: Key<Value2>,
@ -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<Value1>,
_ key2: Key<Value2>,
@ -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<Value1>
) {
@ -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<Value1>,
_ key2: NSSecureCodingKey<Value2>
@ -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<Value1>,
_ key2: NSSecureCodingKey<Value2>,
@ -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<Value1>,
_ key2: NSSecureCodingKey<Value2>,
@ -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<Value1>,
_ key2: NSSecureCodingKey<Value2>,
@ -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<Value1>,
_ key2: NSSecureCodingKey<Value2>,
@ -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<Value1>,
_ key2: NSSecureCodingKey<Value2>,
@ -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<Value1>,
_ key2: NSSecureCodingKey<Value2>,
@ -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<Value1>,
_ key2: NSSecureCodingKey<Value2>,
@ -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<Value1>,
_ key2: NSSecureCodingKey<Value2>,
@ -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<Value1>
) {
@ -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<Value1>,
_ key2: NSSecureCodingOptionalKey<Value2>
@ -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<Value1>,
_ key2: NSSecureCodingOptionalKey<Value2>,
@ -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<Value1>,
_ key2: NSSecureCodingOptionalKey<Value2>,
@ -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<Value1>,
_ key2: NSSecureCodingOptionalKey<Value2>,
@ -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<Value1>,
_ key2: NSSecureCodingOptionalKey<Value2>,
@ -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<Value1>,
_ key2: NSSecureCodingOptionalKey<Value2>,
@ -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<Value1>,
_ key2: NSSecureCodingOptionalKey<Value2>,
@ -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<Value1>,
_ key2: NSSecureCodingOptionalKey<Value2>,
@ -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<Value1>,
_ key2: NSSecureCodingOptionalKey<Value2>,

View File

@ -50,6 +50,8 @@ public struct Default<Value: Codable>: 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<Bool>("hasUnicorn", default: false)

View File

@ -81,7 +81,7 @@ extension UserDefaults {
set(try? NSKeyedArchiver.archivedData(withRootObject: value, requiringSecureCoding: true), forKey: key)
}
public subscript<Value: Codable>(key: Defaults.Key<Value>) -> Value {
public subscript<Value>(key: Defaults.Key<Value>) -> 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<Value: NSSecureCoding>(key: Defaults.NSSecureCodingKey<Value>) -> Value {
public subscript<Value>(key: Defaults.NSSecureCodingKey<Value>) -> 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<Value: NSSecureCoding>(key: Defaults.NSSecureCodingOptionalKey<Value>) -> Value? {
public subscript<Value>(key: Defaults.NSSecureCodingOptionalKey<Value>) -> 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)
}
}
}

View File

@ -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