Skip to content

Commit c9fd8da

Browse files
authored
Fix optional URLs (#115)
1 parent e7bd3ca commit c9fd8da

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

Sources/Sharing/SharedKeys/AppStorageKey.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@
343343
}
344344

345345
fileprivate init(_ key: String, store: UserDefaults?) where Value == URL? {
346-
self.init(lookup: OptionalLookup(base: CastableLookup()), key: key, store: store)
346+
self.init(lookup: OptionalLookup(base: URLLookup()), key: key, store: store)
347347
}
348348

349349
fileprivate init(_ key: String, store: UserDefaults?) where Value == Data? {

Tests/SharingTests/AppStorageTests.swift

+9
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@
4242
#expect(url == URL(fileURLWithPath: "/tmp"))
4343
}
4444

45+
@Test func optionalURL() {
46+
@Shared(.appStorage("url")) var url: URL? = URL(fileURLWithPath: "/dev")
47+
#expect(store.url(forKey: "url") == URL(fileURLWithPath: "/dev"))
48+
store.set(URL(fileURLWithPath: "/tmp"), forKey: "url")
49+
#expect(url == URL(fileURLWithPath: "/tmp"))
50+
$url.withLock { $0 = nil }
51+
#expect(url == nil)
52+
}
53+
4554
@Test func data() {
4655
@Shared(.appStorage("data")) var data = Data([4, 2])
4756
#expect(store.data(forKey: "data") == Data([4, 2]))

0 commit comments

Comments
 (0)