'[布尔]'到‘零’始终返回 true - SwiftUI 中的问题

发布于 2025-01-19 04:16:45 字数 3460 浏览 0 评论 0原文

我正在尝试确保用户在单击确认答案之前只能选择一个答案,而不是几个答案,但是我会遇到错误。我知道,如果是真的或错误,则布尔值不能为零,但是我不知道如何解决这个问题。任何帮助将不胜感激:) 错误发生在“ 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 { $0 }}

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 = $0; self.setOptionTwo = false; self.setOptionThree = false })
        let OptionTwo = Binding<Bool>(get: { self.setOptionTwo }, set: { self.setOptionOne = false; self.setOptionTwo = $0; self.setOptionThree = false })
        let OptionThree = Binding<Bool>(get: { self.setOptionThree }, set: { self.setOptionOne = false; self.setOptionTwo = false; self.setOptionThree = $0 })

//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()

}

}
}

I am trying to ensure the user can only select one answer rather than several before clicking confirm answer, however i keep getting an error. I am aware that a boolean value cannot be nil if it is either true or false however I don't know how to get around this. any help would be appreciated :)
The error occurs on line '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 { $0 }}

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 = $0; self.setOptionTwo = false; self.setOptionThree = false })
        let OptionTwo = Binding<Bool>(get: { self.setOptionTwo }, set: { self.setOptionOne = false; self.setOptionTwo = $0; self.setOptionThree = false })
        let OptionThree = Binding<Bool>(get: { self.setOptionThree }, set: { self.setOptionOne = false; self.setOptionTwo = false; self.setOptionThree = $0 })

//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()

}

}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吹泡泡o 2025-01-26 04:16:45

进行比较不是可选

allOptions != nil

nil

!allOptions.isEmpty

Alloptions 代码> true值do

!allOptions.filter{ $0 }.isEmpty

allOptions is not optional to compare it with nil so it's always true comparison instead you need to Replace

allOptions != nil

with

!allOptions.isEmpty

if you need to check availability of a true value do

!allOptions.filter{ $0 }.isEmpty
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文