Post not yet marked as solved
Using the @Query macro under the @Observable macro may not be a very correct use. In this case, I recommend you to change the structure of your code if you can. If you give more information about what you want to do, I will be happy to help :)
If I understand your problem correctly, it would be better to use @Bindable in this structure. More information about Bindable
Using @Bindable
@Bindable var stagione: Stagione
Let's make changes in Textfields and Button
TextField("Categoria", text: $stagione.categoria)
Button("Aggiorna dati") {
dismiss()
}
.disabled(!modifica)
By doing this, the values in the TextFields will be saved without any function. Please feel free to ask your question :)
Post not yet marked as solved
İt's same for me. Any solution?
Thank you sooo much for your answer. That's completely working. I did it like that for design. (I mean else section). And I would be very grateful if you could explain the code. Because it's a little confusing. Thank you again :)
Picker("Marka", selection: $brandIndex) {
Text("Seçin").tag(0)
ForEach(cars.indices, id: \.self) {
Text(cars[$0].brand).tag($0 + 1)
}
}
.onChange(of: brandIndex) {
modelIndex = 0
}
if brandIndex != 0 && modelIndex <= cars[brandIndex - 1].models.count {
Picker("Model", selection: $modelIndex) {
Text("Seçin").tag(0)
ForEach(cars[brandIndex - 1].models.indices, id: \.self) {
Text(cars[brandIndex - 1].models[$0])
.tag($0 + 1)
}
}
} else { // This section for design
Picker("Model", selection: $modelIndex) {
Text("Seçin").tag(0)
}
}
Thank you for your answer @Claude31 . I'm trying to get the vehicle make and model from the user. I have a .json file which has all car brand and models. I'm getting values from viewModel and this is my struct:
struct Car: Decodable, Identifiable {
enum CodingKeys: CodingKey {
case brand
case models
}
var id = UUID()
var brand: String
var models: [String]
}
In the first picker I want to select brand. Then I want to see models according to the brand I chose.
Picker("Brand", selection: $brandIndex) {
Text("Choose").tag(0)
ForEach(cars.indices, id: \.self) {
Text(cars[$0].brand).tag($0 + 1)
}
}
Picker("Model", selection: $modelIndex) {
Text("Choose").tag(0)
if brandIndex != 0 {
ForEach(cars[brandIndex - 1].models.indices, id: \.self) {
Text(cars[brandIndex - 1].models[$0])
.tag($0 + 1)
}
}
}
First, I select a brand and model, then when I change the brand, if the new brand does not have that value, I get the error I mentioned above. I thought that this situation would be solved if the modelIndex variable was set to 0 every time the brand was changed. Thank you so much.
Post not yet marked as solved
Thank you for your support. But how can I clear binding value?