If语句在VStack swiftUI中不起作用

问题描述 投票:0回答:1

我正在尝试显示一些数字,具体取决于我在视图中创建的变量r0。但是,当我尝试运行我的代码时,出现以下错误:编译器无法在合理的时间内对这个表达式进行类型检查。有没有办法解决这个问题?

我正在Swift Playground中运行此程序

struct SecondView : View {
    @Environment(\.presentationMode) var presentationMode
    @EnvironmentObject var model: MyVariables

    var r0 : Double {
        model.rvalues[(model.wearingMaskToggle ? 1:0) + (model.washingHandsToggle ? 2:0) + (model.quarantineToggle ? 8:0) + (model.coverMouthToggle ? 4:0)]
    }

    var body: some View {
        VStack {
            VStack {
                HStack {
                    Button(action: {
                        self.presentationMode.wrappedValue.dismiss()
                    }, label: {
                        Image(systemName: "arrow.left")
                            .font(.system(size: 30))
                            .foregroundColor(Color.blue)

                    }).padding()
                    Spacer()
                }
            }
            GeometryReader { gp in
                ZStack {
                    Image(uiImage: UIImage(named: "\(self.r0).jpg")!).resizable()
                        .frame(width: gp.size.width/1.15, height: gp.size.width/1.91)
                        .overlay(GeometryReader { gp in
                            VStack {
                                if self.r0 > 1 {
                                    Text("\(Int(self.model.slideone))") 
                                        .foregroundColor(Color.black)
                                        .position(x: gp.size.width / 100000, y: gp.size.height / 5.55)

                                    Text("\(Int((self.model.slideone - self.model.slidetwo)/2))")
                                        .foregroundColor(Color.black)
                                        .position(x: gp.size.width / 100000)
                                        .padding(.top, gp.size.height/4.75)

                                    Text("\(Int(self.model.slidetwo))")
                                        .foregroundColor(Color.black)
                                        .position(x: gp.size.width / 100000, y: gp.size.height / 22.5)
                                        .padding(.top, gp.size.height/5.675)
                                } else{
                                    Text("\(Int(self.model.slidetwo))") 
                                        .foregroundColor(Color.black)
                                        .position(x: gp.size.width / 100000, y: gp.size.height / 5.55)

                                    Text("\(Int(self.model.slidetwo/2))")
                                        .foregroundColor(Color.black)
                                        .position(x: gp.size.width / 100000)
                                        .padding(.top, gp.size.height/4.75)
                                }
                            }
                            VStack {
                                HStack {
                                    Text("10") 
                                        .foregroundColor(Color.black)
                                        .position(x: gp.size.width / 5.75, y: gp.size.height / 1.0675)

                                    Text("20") 
                                        .foregroundColor(Color.black)
                                        .position(x: gp.size.width / 5.75, y: gp.size.height / 1.0675)

                                    Text("20") 
                                        .foregroundColor(Color.black)
                                        .position(x: gp.size.width / 6.65, y: gp.size.height / 1.0675)

                                    Text("30") 
                                        .foregroundColor(Color.black)
                                        .position(x: gp.size.width / 8.2, y: gp.size.height / 1.0675)

                                    Text("40") 
                                        .foregroundColor(Color.black)
                                        .position(x: gp.size.width / 10.4, y: gp.size.height / 1.0675)

                                    Text("50") 
                                        .foregroundColor(Color.black)
                                        .position(x: gp.size.width / 14.5, y: gp.size.height / 1.0675)

                                    Text("60") 
                                        .foregroundColor(Color.black)
                                        .position(x: gp.size.width / 22.5, y: gp.size.height / 1.0675)

                                    Text("70") 
                                        .foregroundColor(Color.black)
                                        .position(x: gp.size.width / 56, y: gp.size.height / 1.0675)
                                }
                            }
                            }
                    )
                }.padding(.leading, gp.size.width / 15)
                Spacer()
            }
        }.background(Color.white)
    }
}
swift swiftui swift-playground
1个回答
0
投票

如果错误在r0中,请尝试将其更改为:

 var r0 : Double {
        model.rvalues[(model.wearingMaskToggle ? 1.0 : 0.0) + (model.washingHandsToggle ? 2.0 : 0.0) + (model.quarantineToggle ? 8.0 : 0.0) + (model.coverMouthToggle ? 4.0 : 0.0)]
    }
© www.soinside.com 2019 - 2024. All rights reserved.