diff --git a/Sources/ComposableArchitecture/SharedState/PersistenceKey/AppStorageKey.swift b/Sources/ComposableArchitecture/SharedState/PersistenceKey/AppStorageKey.swift index 4980d0b10d46..6430bc5defc9 100644 --- a/Sources/ComposableArchitecture/SharedState/PersistenceKey/AppStorageKey.swift +++ b/Sources/ComposableArchitecture/SharedState/PersistenceKey/AppStorageKey.swift @@ -298,7 +298,7 @@ extension AppStorageKey: PersistenceKey { ) { _ in guard !SharedAppStorageLocals.isSetting else { return } - didSet(self.store.value(forKey: self.key) as? Value ?? initialValue) + didSet(load(initialValue: initialValue)) } let willEnterForeground: (any NSObjectProtocol)? if let willEnterForegroundNotificationName { @@ -307,7 +307,7 @@ extension AppStorageKey: PersistenceKey { object: nil, queue: nil ) { _ in - didSet(self.store.value(forKey: self.key) as? Value ?? initialValue) + didSet(load(initialValue: initialValue)) } } else { willEnterForeground = nil diff --git a/Tests/ComposableArchitectureTests/AppStorageTests.swift b/Tests/ComposableArchitectureTests/AppStorageTests.swift index b274e0846848..039bcdb19735 100644 --- a/Tests/ComposableArchitectureTests/AppStorageTests.swift +++ b/Tests/ComposableArchitectureTests/AppStorageTests.swift @@ -111,6 +111,16 @@ final class AppStorageTests: XCTestCase { XCTAssertEqual(count, 42) } + func testChangeUserDefaultsDirectly_RawRepresentable() { + enum Direction: String, CaseIterable { + case north, south, east, west + } + @Dependency(\.defaultAppStorage) var defaults + @Shared(.appStorage("direction")) var direction: Direction = .south + defaults.set("east", forKey: "direction") + XCTAssertEqual(direction, .east) + } + func testChangeUserDefaultsDirectly_KeyWithPeriod() { @Dependency(\.defaultAppStorage) var defaults @Shared(.appStorage("pointfreeco.count")) var count = 0