I'm learning how to store custom types in AppStorage
, and came across an issue. In this simplified example, I'm trying to save an empty Int
array to AppStorage once the view is created.
The following code gives me the error, No exact matches in call to initializer
. I know that this error usually means there are mismatching types somewhere, but I'm not sure what the types should be, or how to fix it.
struct test: View {
init() {
let emptyList = [Int]()
guard let encodedList = try? JSONEncoder().encode(emptyList) else { return }
self.storedList = encodedList
}
@AppStorage("stored_list") var storedList: Data //NO EXACT MATCHES TO CALL IN INITIALIZER
//"body" implementation not shown
}
Why is this error occurring, and how can I fix it?
CodePudding user response:
It should be either with default value or optional, so correct variants are
@AppStorage("stored_list") var storedList: Data = Data()
or
@AppStorage("stored_list") var storedList: Data?