diff --git a/Sources/TypedAppStorage/TypedAppStorage.swift b/Sources/TypedAppStorage/TypedAppStorage.swift index 2e780f2..4b11e7b 100644 --- a/Sources/TypedAppStorage/TypedAppStorage.swift +++ b/Sources/TypedAppStorage/TypedAppStorage.swift @@ -18,13 +18,15 @@ public protocol TypedAppStorageValue: Codable, Sendable { public struct TypedAppStorage: DynamicProperty { private var appStorage: AppStorage private var initialValue: Value + private let encoder = JSONEncoder() + private let decoder = JSONDecoder() /// Store and fetch value from the defined store, with a predefined default value. /// /// This default value if defined is preferred over ``TypedAppStorageValue/defaultValue``. public init(wrappedValue: Value, store: UserDefaults? = nil) { initialValue = wrappedValue - let initialData = try? JSONEncoder().encode(wrappedValue) + let initialData = try? encoder.encode(wrappedValue) let initialString = (initialData == nil ? nil : String(data: initialData!, encoding: .utf8)) ?? "" @@ -48,11 +50,11 @@ public struct TypedAppStorage: DynamicProperty { guard let data = appStorage.wrappedValue.data(using: .utf8) else { return initialValue } - return (try? JSONDecoder().decode(Value.self, from: data)) ?? initialValue + return (try? decoder.decode(Value.self, from: data)) ?? initialValue } nonmutating set { guard - let newData = try? JSONEncoder().encode(newValue), + let newData = try? encoder.encode(newValue), let newString = String(data: newData, encoding: .utf8) else { return } appStorage.wrappedValue = newString