diff --git a/README.md b/README.md index a3185d9..04fa618 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,10 @@ 2. [How to use](#section-1) - [Custome Container](#sub-topic-1.1) - [Supported Data](#sub-topic-1.2) -4. [Storing Custome Data](#section-2) +3. [Storing Custome Data](#section-2) - [Example 1 Enums](#sub-topic-2.1) - [Example 2 Custom Array type](#sub-topic-2.2) +4. [RemovingStoredObjects](#section-3) 5. [Author](#conclusion) ## Introduction @@ -100,6 +101,30 @@ init() } @UserDefaults(\.customeData) var customeData: [CustomeData] = [CustomeData()] ``` + +## RemovingStoredObjects +You can also remove the stored value for a specific key using the `DefaultKeys.removeObject(at keyPath: KeyPath, _ container: UserDefaults = .standard)`, as demonstrated below: + +```swift +@UserDefaults(\.testKey) var test = 3 +DefaultKeys.removeObject(\.testKey) +// if you use a custome container provide it to the funcation using this syntax, +// DefaultKeys.removeObject(at keyPath: KeyPath, _ container: UserDefaults = .standard) +``` + +> [!NOTE] +> When you call `test` after removing the stored value, it will give the default value which is equal `3` in this case (which is not stored UserDefaults2 store). +> if it was an `optional` the output will be `nil` in this case. + +if you want to delete all the values in the UserDefaults, you can use `DefaultKeys.removeAllUserDefaultsObjects(for container: UserDefaults = .standard)`: + +```swift +@UserDefaults(\.testKey) var test = 3 +DefaultKeys.removeAllUserDefaultsObjects() +// if you use a custome container provide it to the funcation using this syntax, +// DefaultKeys.removeAllUserDefaultsObjects(for container: UserDefaults = .standard) +``` + And that's all there is to it! 🚀 Enjoy using this Swifty package. ## Author diff --git a/Sources/UserDefaults/DefaultKeys.swift b/Sources/UserDefaults/DefaultKeys.swift index 0cb2957..264ea82 100644 --- a/Sources/UserDefaults/DefaultKeys.swift +++ b/Sources/UserDefaults/DefaultKeys.swift @@ -30,3 +30,23 @@ public struct DefaultKeys { return current[keyPath: key] } } + +//MARK: - Removing Stored Objects +extension DefaultKeys { + /// Clears all the objects stored in the specified container. By default it uses the ``.standard`` container. + /// + /// When you call the variable after removing it, you will get the default value you provided or nil if it was optional. + public static func removeAllUserDefaultsObjects(for container: UserDefaults = .standard) { + if let domainName = Bundle.main.bundleIdentifier { + container.removePersistentDomain(forName: domainName) + container.synchronize() + } + } + + /// Removes the Value at the specified keyPath. + /// + /// When you call the variable after removing it, you will get the default value you provided or nil if it was optional. + public static func removeObject(at keyPath: KeyPath, _ container: UserDefaults = .standard) { + container.removeObject(forKey: DefaultKeys[keyPath]) + } +} diff --git a/Tests/UserDefaultsTests/UserDefaultsTests.swift b/Tests/UserDefaultsTests/UserDefaultsTests.swift index 2211fde..20ce7b0 100644 --- a/Tests/UserDefaultsTests/UserDefaultsTests.swift +++ b/Tests/UserDefaultsTests/UserDefaultsTests.swift @@ -82,6 +82,26 @@ internal final class UserDefaultsTests: XCTestCase { XCTAssertEqual(testValue, expectedResult, "Expected to have the same value got \(testValue)") } + func test_UserDefaultWrapper_removeAllUserDefaultsObjects_givesTheDefault() { + let expectedResult = "2" + @UserDefault(\.testKey) var testValue: String = "2" + + testValue = "5" + DefaultKeys.removeAllUserDefaultsObjects() + + XCTAssertEqual(testValue, expectedResult, "Expected to have the same value got \(testValue)") + } + + func test_UserDefaultWrapper_removeObjectFromUserDefaults_givesTheDefault() { + let expectedResult = "2" + @UserDefault(\.testKey) var testValue: String = "2" + + testValue = "5" + DefaultKeys.removeObject(at: \.testKey) + + XCTAssertEqual(testValue, expectedResult, "Expected to have the same value got \(testValue)") + } + private func deleteArtifactsFromUserDefaults(for key: KeyPath) { UserDefaults.standard.removeObject(forKey: DefaultKeys[key]) }