Eu sou um novato SwiftUI desenvolvedor, e eu estou tendo problemas para criar um modo de exibição que dinamicamente apresenta diferentes catadores baseado em um usuário anterior de selecção no modo de exibição. Para ver o problema, execute o seguinte código no Simulador, alterar o pickerOneSelection para "Seletor de Três" e, em seguida, mudar NOVAMENTE para "Selecionador de Dois" e tentar alterar o pickerTwoSelection. O aplicativo será imediatamente acidente; o erro específico com este brinquedo exemplo é "Thread 1: EXC_BAD_ACCESS (código=1, address=0 x 8)". Qualquer insight que é mais bem-vindo!
Como um aparte, eu olhei para as seguintes perguntas: (SwiftUI hierárquica Seletor com dados dinâmicos falha) e (swiftUi : 2 Colectores em uma tela aplicativo falha com "Índice fora do intervalo"), mas, se possível, eu gostaria de evitar a conversão de meu seqüência de seleção de variáveis (pickerOneSelection, pickerThreeSelection) em números inteiros, que deve, então, ser traduzido de volta para seqüências para obter o texto associado com a seleção do usuário. Se não é possível fazer o que eu estou tentando fazer sem o índice de variáveis, se você pudesse me ajudar a entender por que seria muito útil.
import SwiftUI
struct ContentView: View {
@State var pickerOneSelection: String = "Picker Two"
@State var pickerTwoVisible: Bool = false
@State var pickerTwoSelection: Int = 3
@State var pickerThreeSelection: String = "Option 1"
var pickerOneOptions = ["Picker Two", "Picker Three"]
var pickerThreeOptions = ["Option 1", "Option 2", "Option 3"]
var body: some View {
NavigationView {
Form {
Picker("Picker One selection", selection: $pickerOneSelection) {
ForEach(pickerOneOptions, id: \.self) {
Text($0)
}
} // End of Picker 1
if pickerOneSelection == "Picker Two" {
HStack {
Text("Picker Two selection")
Spacer()
Button(String(pickerTwoSelection)) {
self.pickerTwoVisible.toggle()
}
} // End of HStack
if pickerTwoVisible {
Picker("Picker Two", selection: $pickerTwoSelection) {
ForEach(0..<32, id: \.self) {
Text(String($0))
}
}
.pickerStyle(WheelPickerStyle())
} // End of pickerTwoVisible
} else {
Picker("Picker Three selection", selection: $pickerThreeSelection) {
ForEach(pickerThreeOptions, id: \.self) {
Text($0)
}
} // End of Picker 3
}
} // End of Form
} // End of NavigationView
} // End of body
} // End of ContentView
Muito obrigado pela sua ajuda e a experiência!