【问题标题】:Compiler SwiftUi view with State value带有状态值的编译器 SwiftUi 视图
【发布时间】:2021-08-21 16:51:01
【问题描述】:

下面的 swift 代码负责根据视图中定义的状态值显示视图,如果值为 true,则打印打印,如果不是 true,则不显示任何内容,现在定义状态的值 (Gotocropview) 是作为绑定传递给另一个视图,然后更改为@Bindign,如果我在 ContentView 视图中注释该行,则编译代码,而如果该行未注释,则代码将按照 contentview 中显示的代码执行,它是产生错误:编译器无法在合理的时间内对该表达式进行类型检查;尝试将表达式分解为不同的子表达式,这是因为什么?

注释行:

   /* else if(Gotocropview==true){
                        print("Crop view è true")
                    }*/

Swift 代码:

struct ContentView: View {

    @State var Gotocamera:Bool = false
    @State var Gotoeditcamera:Bool = false
    @State var Gotocropview:Bool=false
    @Environment(\.presentationMode) var presentationMode
    


    var body: some View {
        ZStack {
            if (!Gotocamera && !Gotoeditcamera && !Gotocropview) {
                Color(#colorLiteral(red: 0.9682741117, green: 0.9682741117, blue: 0.9682741117, alpha: 1)).edgesIgnoringSafeArea(.all)

                VStack {
                    Header()

                    Search()
                        .padding(.top, 10)
                    Buttons(Gotoeditcamera: $Gotoeditcamera,Gotocropview: $Gotocropview)
                        .padding(.vertical, 20)
                    Cards()
                        .padding(.top, 20)
                    if(Support.getDevice() == DeviceType.ipad) {
                        Tabbar(Gotocamera: $Gotocamera)
                            .padding(.top, 200)
                    }
                    else{
                        if(Device.IS_IPHONE_XS){
                        Tabbar(Gotocamera: $Gotocamera)
                            .offset(y: 20)
                        }
                        else{
                            Tabbar(Gotocamera: $Gotocamera)
                                .offset(y: 60)                        }
                    }
                    
                }
            }
             else if(Gotocamera==true) {
                CameraView()
            }
            else if(Gotoeditcamera==true){
                ChangeEffect()
             }
            else if(Gotocropview==true){
                print("Crop view è true")
            }
        }
    }
}

struct Buttons: View {
    @Binding var Gotoeditcamera:Bool
    @Binding var Gotocropview:Bool
...

}

【问题讨论】:

    标签: ios swift swiftui swift5


    【解决方案1】:

    由于没有显示完整的源代码,很难说,但是如果你只是想消除错误,你可以这样做。

    let _ = print("Crop view è true")
    

    见:How to print() to Xcode console in SwiftUI?


    重现错误的最少代码如下。

    import SwiftUI
    
    struct ContentView: View {
        @State var Gotocamera:Bool = false
        @State var Gotoeditcamera:Bool = false
        @State var Gotocropview:Bool=false
        @Environment(\.presentationMode) var presentationMode
        
        var body: some View {
            ZStack {
                if (!Gotocamera && !Gotoeditcamera && !Gotocropview) {
                    VStack {
                    }
                }
                else if(Gotocamera==true) {
                }
                else if(Gotoeditcamera==true){
                }
                else if(Gotocropview==true){
                    print("Crop view è true")
                }
            }
        }
    }
    

    编译器无法在合理的时间内对该表达式进行类型检查;尝试将表达式分解为不同的子表达式

    如果你把它改成以下,错误就会消失。

    import SwiftUI
    
    struct ContentView: View {
        @State var Gotocamera:Bool = false
        @State var Gotoeditcamera:Bool = false
        @State var Gotocropview:Bool=false
        @Environment(\.presentationMode) var presentationMode
        
        var body: some View {
            ZStack {
                if (!Gotocamera && !Gotoeditcamera && !Gotocropview) {
                    VStack {
                    }
                }
                else if(Gotocamera==true) {
                }
                else if(Gotoeditcamera==true){
                }
                else if(Gotocropview==true){
                    let _ = print("Crop view è true") // fixed
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-10-05
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      • 2020-10-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多