SwiftUI picker selection

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:
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.
Answered by OOPer in 646823022

I would like to store ether assign e.g. typeStreet variable with the result of the picker selection or just return the selected one.

Seems your code is too simplified to guess when or where you want to use the value.

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.)

Sorry, but it is completely unclear.

Sorry for being so unclear.

What is it which doesn't work?
What do you mean by doesn't work?
It does not compile? It compiles but causes runtime errors? It compiles and runs without errors but shows unexpected results?


I just don't understand how to do it.

Is it possible exchange about this subject elsewhere ? more easily to explain one another what it's going wrong ?

how to do it. 

Please explain what is it.

Is it possible exchange about this subject elsewhere ? more easily to explain one another what it's going wrong ?

It is useless to find elsewhere if you continue to refuse answering to my questions.

Using publisher for an Array literal does not work as expected in almost all cases

This is what I don't understand. How am I supposed to do it ?

Also, the difference between this two way :
Code Block Swift
@StateObject var newPatient = NewPatient()

Code Block Swift
@ObservedObject var newPatient: NewPatient


I'm exhausted of trying about this issue I'm facing for some time now. If you wouldn't mind explain it a bit easier, I'd be grateful.

Thanks a lot again

If you wouldn't mind explain it a bit easier, I'd be grateful.

If you had shown enough info by answering my questions, I would be very glad to explain.
I find the solution ! 😇

I will honest, it is quit dirty but it is working !

Code Block Swift
Picker("Street desc.", selection: $patient.indexStreet) {
ForEach (0 ..< NewPatient.typeStrt.count) {
Text(NewPatient.typeStrt[$0])
}
}
.pickerStyle(SegmentedPickerStyle())
.onAppear(perform: {
self.patient.typeStreet = NewPatient.typeStrt[self.patient.indexStreet]
})


Regards and thank you for your help.

 it is working !

Thanks for sharing your solution.
Frankly, I do not understand what was your problem even seeing your working code...

But anyway, I will do my best when you would post another issue in the future.
May your app be great.
SwiftUI picker selection
 
 
Q