当我尝试将 Toggle 的输出分配给一个旧变量时,它说:Binding<Bool> is not convertible to <Bool>
When I try and assign the output of the Toggle to an old variable it says: Binding<Bool> is not convertible to <Bool>
当我尝试将 Toggle 的输出分配给一个旧变量时,它说:Binding is not convertible to bool。有没有办法来解决这个问题。我正在 Swift Playgrounds 中对此进行编码。下面的代码是不起作用的部分
import SwiftUI
import PlaygroundSupport
struct ContentView : View {
@State private var redButton = false
@State private var step = 0
@State private var washingHandsToggle = true
@State private var wearingMaskToggle = true
@State private var coverMouthToggle = true
@State private var quarantineToggle = true
@State private var text = ""
@State private var slide = 20.0
@State private var slidetwo = 500000.0
@State private var r0 = 0
@State private var rvalues = [6.5, 6.1, 5.3, 4.8, 4.2, 3.5, 3.1, 2.9, 1.75, 1.5, 1.2, 1.0, 0.92, 0.82, 0.75, 0.7]
var body: some View {
VStack {
VStack {
Toggle(isOn: $washingHandsToggle) {
Text("Washing Hands Regularly")
}.padding(.horizontal, 50)
.padding(.vertical, 25)
Toggle(isOn: $wearingMaskToggle) {
Text("Wearing a Protective Mask")
}.padding(.horizontal, 50)
.padding(.vertical, 25)
Toggle(isOn: $coverMouthToggle) {
Text("Cover Mouth When Sneezing")
}.padding(.horizontal, 50)
.padding(.vertical, 25)
Toggle(isOn: $quarantineToggle) {
Text("Quarantine")
}.padding(.horizontal, 50)
.padding(.vertical, 25)
.padding(.bottom, 30)
r0 = rvalues[(wearingMaskToggle ? 1:0) + (washingHandsToggle ? 2:0) + (quarantineToggle ? 8:0) + (coverMouthToggle ? 4:0)]
if !washingHandsToggle && !wearingMaskToggle && !quarantineToggle {
Text("Select At least 1")
}else {
Text("Your R") + Text("0")
.font(.system(size: 15.0))
.baselineOffset(-6.0) + Text("Value Is")
Text("\(r0)")
}
您不能只在 VStack
中间设置 r0
。通常,如果您开始在函数构建器中编写赋值、循环、方法调用等普通语句,例如 VStack.init
.
的闭包参数,它会变得非常混乱
您最好将 r0
计算为 属性:
var r0 : Double {
rvalues[(wearingMaskToggle ? 1:0) + (washingHandsToggle ? 2:0) + (quarantineToggle ? 8:0) + (coverMouthToggle ? 4:0)]
}
并删除设置r0
的行。
else
分支中的四个 Text
之一缺少 +
。你应该写:
Text("Your R") +
Text("0 ")
.font(.system(size: 15.0))
.baselineOffset(-6.0) +
Text("Value Is ") +
Text("\(r0)")
此外,我注意到您没有在 if 语句中包含 coverMouthToggle
。这可能是一个错字...
当我尝试将 Toggle 的输出分配给一个旧变量时,它说:Binding is not convertible to bool。有没有办法来解决这个问题。我正在 Swift Playgrounds 中对此进行编码。下面的代码是不起作用的部分
import SwiftUI
import PlaygroundSupport
struct ContentView : View {
@State private var redButton = false
@State private var step = 0
@State private var washingHandsToggle = true
@State private var wearingMaskToggle = true
@State private var coverMouthToggle = true
@State private var quarantineToggle = true
@State private var text = ""
@State private var slide = 20.0
@State private var slidetwo = 500000.0
@State private var r0 = 0
@State private var rvalues = [6.5, 6.1, 5.3, 4.8, 4.2, 3.5, 3.1, 2.9, 1.75, 1.5, 1.2, 1.0, 0.92, 0.82, 0.75, 0.7]
var body: some View {
VStack {
VStack {
Toggle(isOn: $washingHandsToggle) {
Text("Washing Hands Regularly")
}.padding(.horizontal, 50)
.padding(.vertical, 25)
Toggle(isOn: $wearingMaskToggle) {
Text("Wearing a Protective Mask")
}.padding(.horizontal, 50)
.padding(.vertical, 25)
Toggle(isOn: $coverMouthToggle) {
Text("Cover Mouth When Sneezing")
}.padding(.horizontal, 50)
.padding(.vertical, 25)
Toggle(isOn: $quarantineToggle) {
Text("Quarantine")
}.padding(.horizontal, 50)
.padding(.vertical, 25)
.padding(.bottom, 30)
r0 = rvalues[(wearingMaskToggle ? 1:0) + (washingHandsToggle ? 2:0) + (quarantineToggle ? 8:0) + (coverMouthToggle ? 4:0)]
if !washingHandsToggle && !wearingMaskToggle && !quarantineToggle {
Text("Select At least 1")
}else {
Text("Your R") + Text("0")
.font(.system(size: 15.0))
.baselineOffset(-6.0) + Text("Value Is")
Text("\(r0)")
}
您不能只在 VStack
中间设置 r0
。通常,如果您开始在函数构建器中编写赋值、循环、方法调用等普通语句,例如 VStack.init
.
您最好将 r0
计算为 属性:
var r0 : Double {
rvalues[(wearingMaskToggle ? 1:0) + (washingHandsToggle ? 2:0) + (quarantineToggle ? 8:0) + (coverMouthToggle ? 4:0)]
}
并删除设置r0
的行。
else
分支中的四个 Text
之一缺少 +
。你应该写:
Text("Your R") +
Text("0 ")
.font(.system(size: 15.0))
.baselineOffset(-6.0) +
Text("Value Is ") +
Text("\(r0)")
此外,我注意到您没有在 if 语句中包含 coverMouthToggle
。这可能是一个错字...