'[Bool]' 到 'nil' 总是 returns true - SwiftUI 中的问题

'[Bool]' to 'nil' always returns true - Issue in SwiftUI

我试图确保用户在单击确认答案之前只能 select 一个答案而不是多个答案,但是我一直收到错误消息。我知道布尔值不能为 nil,如果它是真或假,但我不知道如何解决这个问题。任何帮助,将不胜感激 :) 错误发生在 'allOptions != nil}'

import SwiftUI
struct SecondView: View {


//Creating Variables for Revision Topics
@State private var setOptionOne = false
@State private var setOptionTwo = false
@State private var setOptionThree = false
    
@State private var questionIndex = 0

let button = ["Confirm Answer"]
@State public var buttonConfirm = [Int?]()

private var allOptions: [Bool] {
   [setOptionOne, setOptionTwo, setOptionThree]}

private var oneOption: Bool {
    allOptions.contains { [=12=] }}

private var isQuestionValid: Bool {
    allOptions != nil}

        var body: some View {
            
        ScrollView{
            VStack(spacing: 1.0) {
                
    Group {
        VStack {
        Text(ResearchMCQ[questionIndex].question)
            .padding(EdgeInsets(top: 10, leading: 10, bottom: 0, trailing: 10))
        }
    
//Ensures Only One Answer Can Be Selected
        let OptionOne = Binding<Bool>(get: { self.setOptionOne }, set: { self.setOptionOne = [=12=]; self.setOptionTwo = false; self.setOptionThree = false })
        let OptionTwo = Binding<Bool>(get: { self.setOptionTwo }, set: { self.setOptionOne = false; self.setOptionTwo = [=12=]; self.setOptionThree = false })
        let OptionThree = Binding<Bool>(get: { self.setOptionThree }, set: { self.setOptionOne = false; self.setOptionTwo = false; self.setOptionThree = [=12=] })

//Shows User MCQ Options
        VStack {
            Toggle(ResearchMCQ[questionIndex].options[0], isOn: OptionOne)
                .toggleStyle(.button)
                .tint(Color(red: 0.8, green: 0.8, blue: 0.8))
                .foregroundColor(Color("Black-White"))
            Toggle(ResearchMCQ[questionIndex].options[1], isOn: OptionTwo)
                .toggleStyle(.button)
                .tint(Color(red: 0.8, green: 0.8, blue: 0.8))
                .foregroundColor(Color("Black-White"))
            Toggle(ResearchMCQ[questionIndex].options[2], isOn: OptionThree)
                .toggleStyle(.button)
                .tint(Color(red: 0.8, green: 0.8, blue: 0.8))
                .foregroundColor(Color("Black-White"))
                 }
    }
                
                HStack(spacing: 15) {
                ForEach(0..<button.count, id: \.self) {button in
                    Button {

// Make sure the index doesn't go beyond the array size
                        if ResearchMCQ.count > questionIndex + 1 {
                            questionIndex += 1
                        }
                    } label: {
                        Text("Confirm Answer")
                    }
                            
                    .padding(.vertical, 12.5)
                    .padding(.horizontal, 120)
                    .foregroundColor(.white)
                    .foregroundStyle(.background)
                    .clipShape(Capsule())
                    .background(2 == button ? Color.primary: Color.secondary)
                    .clipShape(Capsule()).disabled(!isQuestionValid)

        }
    }
}
            
//Allows Header To Be Displayed
   .navigationTitle("Research Methods Year 1 & 2")
   .navigationBarBackButtonHidden(true)
   .navigationBarTitleDisplayMode(.inline)
            
        }

}

        
struct SecondView_Previews: PreviewProvider {
static var previews: some View {
    SecondView()

}

}
}

allOptionsnil 比较不是可选的,所以它总是 true 比较而不是你需要替换

allOptions != nil

!allOptions.isEmpty

如果您需要检查 true 值的可用性,请执行

!allOptions.filter{ [=12=] }.isEmpty