diff --git a/Sources/Defaults.swift b/Sources/Defaults.swift index 8090830..d10dd2f 100644 --- a/Sources/Defaults.swift +++ b/Sources/Defaults.swift @@ -104,9 +104,12 @@ public extension UserDefaults { return _get(key.name) } set { - if let value = newValue { - _set(key.name, to: value) + guard let value = newValue else { + set(nil, forKey: key.name) + return } + + _set(key.name, to: value) } } diff --git a/Tests/DefaultsTests/DefaultsTests.swift b/Tests/DefaultsTests/DefaultsTests.swift index 5356a99..dcf04ea 100644 --- a/Tests/DefaultsTests/DefaultsTests.swift +++ b/Tests/DefaultsTests/DefaultsTests.swift @@ -35,6 +35,10 @@ final class DefaultsTests: XCTestCase { XCTAssertNil(UserDefaults.standard[key]) UserDefaults.standard[key] = true XCTAssertTrue(UserDefaults.standard[key]!) + UserDefaults.standard[key] = nil + XCTAssertNil(UserDefaults.standard[key]) + UserDefaults.standard[key] = false + XCTAssertFalse(UserDefaults.standard[key]!) } func testKeys() {