diff --git a/Sources/Defaults/SwiftUI.swift b/Sources/Defaults/SwiftUI.swift index 8a7b219..bec3503 100644 --- a/Sources/Defaults/SwiftUI.swift +++ b/Sources/Defaults/SwiftUI.swift @@ -87,6 +87,9 @@ public struct Default: DynamicProperty { public var projectedValue: Binding { $observable.value } + /// The default value of the key. + public var defaultValue: Value { key.defaultValue } + /// Combine publisher that publishes values when the `Defaults` item changes. public var publisher: Publisher { Defaults.publisher(key) } @@ -118,6 +121,12 @@ public struct Default: DynamicProperty { } } +@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) +extension Default where Value: Equatable { + /// Indicates whether the value is the same as the default value. + public var isDefaultValue: Bool { wrappedValue == defaultValue } +} + @available(macOS 11, iOS 14, tvOS 14, watchOS 7, *) extension Defaults { /** diff --git a/Tests/DefaultsTests/DefaultsSwiftUITests.swift b/Tests/DefaultsTests/DefaultsSwiftUITests.swift index 4e1ace5..987c27f 100644 --- a/Tests/DefaultsTests/DefaultsSwiftUITests.swift +++ b/Tests/DefaultsTests/DefaultsSwiftUITests.swift @@ -44,5 +44,7 @@ final class DefaultsSwiftUITests: XCTestCase { XCTAssertTrue(view.hasUnicorn) XCTAssertEqual(view.user.username, "Chen") XCTAssertEqual(view.setInt, Set(1...4)) + XCTAssertFalse(Default(.hasUnicorn).defaultValue) + XCTAssertFalse(Default(.hasUnicorn).isDefaultValue) } }