@@ -193,7 +193,7 @@ public struct AppStorageKey<Value> {
193193
194194 public init ( _ key: String ) where Value == URL {
195195 @Dependency ( \. defaultAppStorage) var store
196- self . lookup = CastableLookup ( )
196+ self . lookup = URLLookup ( )
197197 self . key = key
198198 self . store = store
199199 }
@@ -251,7 +251,7 @@ public struct AppStorageKey<Value> {
251251
252252 public init ( _ key: String ) where Value == URL ? {
253253 @Dependency ( \. defaultAppStorage) var store
254- self . lookup = OptionalLookup ( base: CastableLookup ( ) )
254+ self . lookup = OptionalLookup ( base: URLLookup ( ) )
255255 self . key = key
256256 self . store = store
257257 }
@@ -384,6 +384,30 @@ private struct CastableLookup<Value>: Lookup {
384384 }
385385}
386386
387+ /// Lookup implementation tuned for URL values.
388+ /// For URLs, dedicated UserDefaults APIs for getting/setting need to be called that convert the URL from/to Data.
389+ /// Calling setValue with a URL causes a NSInvalidArgumentException exception.
390+ private struct URLLookup : Lookup {
391+ typealias Value = URL
392+
393+ func loadValue( from store: UserDefaults , at key: String , default defaultValue: URL ? ) -> URL ? {
394+ guard let value = store. url ( forKey: key)
395+ else {
396+ SharedAppStorageLocals . $isSetting. withValue ( true ) {
397+ store. set ( defaultValue, forKey: key)
398+ }
399+ return defaultValue
400+ }
401+ return value
402+ }
403+
404+ func saveValue( _ newValue: URL , to store: UserDefaults , at key: String ) {
405+ SharedAppStorageLocals . $isSetting. withValue ( true ) {
406+ store. set ( newValue, forKey: key)
407+ }
408+ }
409+ }
410+
387411private struct RawRepresentableLookup < Value: RawRepresentable , Base: Lookup > : Lookup
388412where Value. RawValue == Base . Value {
389413 let base : Base
0 commit comments