'[布尔]'到‘零’始终返回 true - SwiftUI 中的问题
我正在尝试确保用户在单击确认答案之前只能选择一个答案,而不是几个答案,但是我会遇到错误。我知道,如果是真的或错误,则布尔值不能为零,但是我不知道如何解决这个问题。任何帮助将不胜感激:) 错误发生在“ 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
进行比较不是可选
与
nil
Alloptions 代码> true
值doallOptions
is not optional to compare it withnil
so it's alwaystrue
comparison instead you need to Replacewith
if you need to check availability of a
true
value do