Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Sources/UserDefaultsDependency/UserDefaults+Specializations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ extension UserDefaults.Dependency {
self._set(value, key)
}

/// Returns the Dictionary value associated with the specified key.
/// - Parameter key: A key in the current user defaults store.
/// - Returns: The Dictionary value associated with the specified key, or `nil` if there is no value
/// associated to `key`
public func dictionary(forKey key: String) -> [String: any Sendable]? {
self._get(key, Dictionary<String, any Sendable>.self) as? [String: any Sendable]
}

/// Sets the value of the specified default key to the specified Dictionary value.
/// - Parameters:
/// - value: The Dictionary value to store in the user's defaults store. If the value is `nil`,
/// the associated value will be removed from the store.
/// - key: The key with which to associate the value.
public func set(_ value: [String: any Sendable]?, forKey key: String) {
self._set(value, key)
}

/// Returns the Double value associated with the specified key.
/// - Parameter key: A key in the current user defaults store.
/// - Returns: The Double value associated with the specified key, or `nil` if there is no value
Expand Down
4 changes: 4 additions & 0 deletions Sources/UserDefaultsDependency/UserDefaultsDependency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ extension UserDefaults {
return self.data(forKey: key)
case let type where type == Date.self, let type where type == Date?.self:
return self.object(forKey: key) as? Date
case let type where type == Dictionary<String, any Sendable>.self, let type where type == Dictionary<String, any Sendable>?.self:
return self.dictionary(forKey: key)
case let type where type == Double.self, let type where type == Double?.self:
guard self.contains(key: key) else { return nil }
return self.double(forKey: key)
Expand Down Expand Up @@ -176,6 +178,8 @@ extension UserDefaults {
self.set(value, forKey: key)
case let value as Date:
self.set(value, forKey: key)
case let value as Dictionary<String, any Sendable>:
self.set(value, forKey: key)
case let value as Double:
self.set(value, forKey: key)
case let value as Int:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ final class UserDefaultsDependencyTests: XCTestCase {
UserDefaults.standard.removeObject(forKey: "date")
}

func testLiveUserDefaultsDictionary() {
@Dependency(\.userDefaults) var userDefaults: UserDefaults.Dependency
let dict: [String: any Sendable] = ["key1": "value1", "key2": "value2"]
UserDefaults.standard.removeObject(forKey: "dictionary")
withDependencies {
$0.userDefaults = .standard
} operation: {
userDefaults.set(dict, forKey: "dictionary")
}
withDependencies {
$0.userDefaults = .standard
} operation: {
XCTAssertEqual(userDefaults.dictionary(forKey: "dictionary") as? [String: String], ["key1": "value1", "key2": "value2"])
}
UserDefaults.standard.removeObject(forKey: "dictionary")
}

func testLiveUserDefaultsDouble() {
@Dependency(\.userDefaults) var userDefaults: UserDefaults.Dependency
let double = 123.4
Expand Down