Hello,
I am beginning with swift and I'm stuck with a picker issue.
I need to fill a JSON with the selected choice. And it keeps returning me the array.
Here is the Class:
Here is the View code:
I would like to store ether assign e.g. typeStreet variable with the result of the picker selection or just return the selected one.
Thank you for your help,
P.
I am beginning with swift and I'm stuck with a picker issue.
I need to fill a JSON with the selected choice. And it keeps returning me the array.
Here is the Class:
Code Block Swift class NewPatient: ObservableObject, Codable { enum CodingKeys: String, CodingKey { case lastName, firstName, age, phoneNumber, emailAddr = "email", address, streetNumber, street, typeStreetNumber, typeStreet } @Published var firstName: String = "" @Published var lastName: String = "" @Published var age: Int? static let typeStreetNbr = ["", "bis", "ter"] @Published var typeStreetNumber: String = "" @Published var indexStreetNbr = 0 static let typeStrt = ["rue", "boulevard", "avenue", "chemin"] @Published var typeStreet: String = "" @Published var indexStreet = 0 init() { } required init(from decoder: Decoder) throws { lastName = try container.decode(String.self, forKey: .lastName) firstName = try container.decode(String.self, forKey: .firstName) age = try container.decode(Int.self, forKey: .age) let address = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .address) typeStreetNumber = try address.decode(String.self, forKey: .typeStreetNumber) typeStreet = try address.decode(String.self, forKey: .typeStreet) } func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(firstName, forKey: .firstName) try container.encode(lastName, forKey: .lastName) try container.encode(age, forKey: .age) var address = container.nestedContainer(keyedBy: CodingKeys.self, forKey: .address) try address.encode(typeStreetNumber, forKey: .typeStreetNumber) try address.encode(typeStreet, forKey: .typeStreet) } }
Here is the View code:
Code Block Swift struct SignUpClient: View { @State private var backPressed: Bool = false var body: some View { if backPressed { return AnyView(PreSignUp()) } else { return AnyView(SignUpClientView(backPressed: $backPressed, patient: NewPatient())) } } } struct SignUpClientView: View { @Binding var backPressed: Bool @State private var patientAddressInfo: Bool = false @ObservedObject var patient: NewPatient var body: some View { Form { Section { Picker("Street desc.", selection: $patient.indexStreet) { ForEach (0 ..< NewPatient.typeStrt.count) { Text(NewPatient.typeStrt[$0]) } }.pickerStyle(SegmentedPickerStyle()) } } }
I would like to store ether assign e.g. typeStreet variable with the result of the picker selection or just return the selected one.
Thank you for your help,
P.
Seems your code is too simplified to guess when or where you want to use the value.I would like to store ether assign e.g. typeStreet variable with the result of the picker selection or just return the selected one.
But anyway, with this line of code:
Code Block return AnyView(SignUpClientView(backPressed: $backPressed, patient: NewPatient()))
You create a new instance of NewPatient, and disposing it (which may contain the result of selection) immediately.
You may need to keep the instance somewhere till you use all the result inside it.
Code Block struct SignUpClient: View { @State private var backPressed: Bool = false @StateObject var newPatient = NewPatient() //<- Keep the newly created instance in an `@StateObject`. var body: some View { VStack { Text("\(newPatient.indexStreet)") if backPressed { PreSignUp() } else { SignUpClientView(backPressed: $backPressed, patient: newPatient) } } } }
(Assuming you use Xcode 12. If you are using an older version, please tell us.)