I am pulling my hair out trying to figure out how to get my picker to show the already stored CoreData value. I want it to show on the right side of the picker as if the user just selected it. I have tried adding self.updatedItemAttribute = self.editItem.attribute ?? "" prior to the picker to set the initial value but that does not build. I also tried defining it in @State (e.g. @State var updatedItemAttribute: String = self.editItem.attribute) but that does not build either. If I add a TextField prior to the picker it will set the value, but I do not want to have a TextField with the value just to get it to set. Any ideas on how I get updatedItemAttribute set to editItem.attribute just prior to the picker? Thanks.
import CoreData import SwiftUI struct EditItemView: View { @Environment(\.managedObjectContext) var moc @Environment(\.presentationMode) var presentationMode @ObservedObject var editItem: Item @State var updatedItemName: String = "" @State var updatedItemAttribute: String = "" let attributes = ["Red", "Purple", "Yellow", "Gold"] var body: some View { NavigationView { Form { Section { TextField("Name of item", text: $updatedItemName) .onAppear { self.updatedItemName = self.editItem.name ?? "" } Picker("Test attribute", selection: self.$updatedItemAttribute) { ForEach(attributes, id: \.self) { Text($0) .onAppear { self.updatedItemAttribute = self.editItem.attribute ?? "" } } } } ...