I’ve a SwiftUI view with a Picker that lists a set of objects, whose id are of kind UUIDs. For the aim of lowering the instance to the minimal code, I’ve represented the entire object as UUIDs. In the true software, the array of objects comes from the surroundings, however it’s not necessary to the query.
When rendering the view, I get the next warning:
“Picker: the choice “nil” is invalid and doesn’t have an related tag, this can give undefined outcomes.”
How can I initialize the chosen worth for the picker, which is saved to SceneStorage, in order that I do away with the warning. Within the instance the saving to SceneStorage is ineffective, however in the true software is a requirement, so I can’t do away with it.
Right here is my code:
struct ContentView: View {
non-public var values: [UUID] = [ UUID(), UUID()]
@SceneStorage("VALUE") non-public var selectedValue: UUID?
var physique: some View {
Type {
Part("Check") {
Picker("Choose a price", choice: $selectedValue) {
//Textual content(values[0].uuidString).tag(values[0])
//Textual content(values[1].uuidString).tag(values[1])
ForEach(values, id: .self) { worth in
Textual content(worth.uuidString).tag(worth)
}
}
//.pickerStyle(.inline)
.pickerStyle(.menu)
}
}
.onAppear() {
self.selectedValue = values[1] as UUID?
}
}
}
extension UUID: RawRepresentable {
public typealias RawValue = String
public init?(rawValue: RawValue) {
self.init(uuidString: rawValue)
}
public var rawValue: String {
self.uuidString
}
}
My understanding is that the issue arises from the truth that the property selectedValue is Optionally available and SwiftUI is making an attempt to render the view when its worth has not but been set. How can I do away with the warning? Is there any strategy to initialize a SeceneStorage property worth earlier than onAppear, like on init?
I need to notice additionally that such warning isn’t proven if the picker fashion is modified to .inline.
Thanks!