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()
}
}
}
>Solution :
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