Skip to content

Commit

Permalink
Feature remove objects (#8)
Browse files Browse the repository at this point in the history
* RemoveObject feature

* Update README.md

Updated the  README.md with the new feature
  • Loading branch information
EngOmarElsayed authored May 29, 2024
1 parent 8936fb8 commit df194a0
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a name="introduction"></a>
Expand Down Expand Up @@ -100,6 +101,30 @@ init()
}
@UserDefaults(\.customeData) var customeData: [CustomeData] = [CustomeData()]
```

## RemovingStoredObjects <a name="section-3"></a>
You can also remove the stored value for a specific key using the `DefaultKeys.removeObject(at keyPath: KeyPath<DefaultKeys, String>, _ 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<DefaultKeys, String>, _ 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 <a name="conclusion"></a>
Expand Down
20 changes: 20 additions & 0 deletions Sources/UserDefaults/DefaultKeys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<DefaultKeys, String>, _ container: UserDefaults = .standard) {
container.removeObject(forKey: DefaultKeys[keyPath])
}
}
20 changes: 20 additions & 0 deletions Tests/UserDefaultsTests/UserDefaultsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<DefaultKeys, String>) {
UserDefaults.standard.removeObject(forKey: DefaultKeys[key])
}
Expand Down

0 comments on commit df194a0

Please sign in to comment.