如何将Swift UI代码中的表达式分解为不同的子表达式?

问题描述

如何对代码进行分组以解决错误,

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

我在堆栈溢出上尝试了所有其他方法,但是它们只会在我的代码中引起更多错误。注意-此页面的目的是显示不同位置上的信息,并在选择位置后为英雄视图(弹出视图)提供更多信息。

这是我的代码...

struct MainView : View {
            
            @State var topIndex = 0
            @State var index = 0
            @State var show = false
            @State var selectedIndex = 0
            // hero Animation....
            @Namespace var name
               
            var body: some View{
                
                ZStack {
                        Image("background1")
                            .resizable()
                            .scaledToFill()
                            .edgesIgnoringSafeArea(.all)
                    
                
                ZStack{
                    
                    VStack(spacing: 0){
                        
                        // tab view with tabs....
                        
                        // change tabs based on index...
                        
                        // Scroll View For Smaller Size Phones....
                        
                        ScrollView(UIScreen.main.bounds.height < 750 ? .vertical : .init(),showsIndicators: false) {
                            
                            VStack{
                                
                                
                                ScrollView(.horizontal,showsIndicators: false) {
                                    
                                    HStack(spacing: 30){
                                    
                                        }
                                    }
                                    .padding(.horizontal)
                                }
                                .padding(.top,60)
                                
                                // Vertical Menu...
                                
                                HStack{
                                    
                                    VerticalMenu()
                                        // moving view to left...
                                        .padding(.leading,-165)
                                        .zIndex(1)
                                    // moving view in stack for click event...
                                    
                                    // Scroll view....
                                    
                                    ScrollView(.horizontal,showsIndicators: false) {
                                        
                                        HStack(spacing: 15){
                                            
                                            ForEach(landmarkData,id: \.id) { landmark in
                                                
                                                ZStack(alignment: Alignment(horizontal: .center,vertical: .top)){
                            
                                                    VStack(alignment: .leading){
                                                        
                                                        Image(landmark.image)
                                                            .resizable()
                                                            .aspectRatio(contentMode: .fit)
                                                        // rotaing image....
                                                            .matchedGeometryEffect(id: landmark.image,in: name)
                                                        
                                                        Spacer(minLength: 0)
                                                        
                                                        Text(landmark.name)
                                                            .font(.system(size: 22))
                                                            .fontWeight(.bold)
                                                            .foregroundColor(.black)
                                                    
                                                        Spacer(minLength: 0)
                                                        
                                                        Text(landmark.category)
                                                            .foregroundColor(Color.white.opacity(0.6))
                                                        
                                                        Text(landmark.park)
                                                            .font(.system(size: 22))
                                                            .fontWeight(.bold)
                                                            .foregroundColor(.black)
                                                    }
                                                    
                                                    VStack(alignment: .leading){
                                                        
                                                        Image(landmark.image)
                                                            .resizable()
                                                            .aspectRatio(contentMode: .fit)
                                                        // rotaing image....
                                                            .matchedGeometryEffect(id: landmark.image,in: name)
                                                        
                                                        Spacer(minLength: 0)
                                                        
                                                        Text(landmark.name)
                                                            .font(.system(size: 22))
                                                            .fontWeight(.bold)
                                                            .foregroundColor(.black)
                                                    
                                                        Spacer(minLength: 0)
                                                        
                                                        Text(landmark.category)
                                                            .foregroundColor(Color.white.opacity(0.6))
                                                        
                                                        Text(landmark.city)
                                                            .font(.system(size: 22))
                                                            .fontWeight(.bold)
                                                            .foregroundColor(.black)
                                                    }
                                                }
                                                .padding(.horizontal)
                                                .padding(.vertical)
                                                // fixed Frame...
                                                .frame(width: UIScreen.main.bounds.width - 150,height: 360)
                                                .background(Color(.white))
                                                // opening hero animation...
                                                .onTapGesture {
                                                    
                                                    withAnimation(.spring()){
                                                        
                                                        selectedIndex = i
                                                        show.toggle()
                                                    }
                                                }
                                            }
                                        }
                                        .padding(.leading,20)
                                        .padding(.trailing)
                                    }
                                    .padding(.leading,-165)
                                }
                                // fixed height...
                                .padding(.top,30)
                                .frame(height: 400)
                                // when view is rotated width is converted to height,...
                                
                            
                                .padding(.top,25)
                                .padding(.horizontal)
                                
                                // Menu...
                                
                                }
                                .padding(.top,20)
                        .padding(.horizontal)
                                
                                Spacer(minLength: 0)
                                
                                
                            }
                            // since all edges are ignored....
                            .padding(.top,UIApplication.shared.windows.first?.safeAreaInsets.top)
                            .padding(.bottom)
                        }
                        
                        
                    
                    // Hero View....
                    
                    if show{
                        
                        VStack{
                            
                            VStack{
                                
                                HStack{
                                    
                                    Button(action: {}) {
                                        
                                        Image(systemName: "suit.heart")
                                            .font(.system(size: 22))
                                            .foregroundColor(.white)
                                    }
                                    
                                    Spacer()
                                    
                                    Button(action: {
                                        
                                        // closing hero view...
                                        
                                        withAnimation(.spring()){
                                            
                                            show.toggle()
                                        }
                                        
                                    }) {
                                        
                                        Image(systemName: "xmark")
                                            .font(.system(size: 22))
                                            .foregroundColor(.white)
                                    }
                                }
                                
                                Image(landmark.image)
                                    .resizable()
                                    .aspectRatio(contentMode: .fit)
                                    .frame(height: 250)
                                    .matchedGeometryEffect(id: landmark.image,in: name)
                                    .rotationEffect(.init(degrees: 12))
                                    .padding(.horizontal)
                            }
                            .padding()
                            .padding(.top,UIApplication.shared.windows.first?.safeAreaInsets.top)
                            .background(Color(.white))
                            
                            ScrollView(UIScreen.main.bounds.height < 750 ? .vertical : .init(),showsIndicators: false) {
                                
                                HStack{
                                    
                                    VStack(alignment: .leading,spacing: 10) {
                                        
                                        Text(landmark.category)
                                            .foregroundColor(.gray)
                                        
                                        Text(landmark.park)
                                            .font(.system(size: 22))
                                            .fontWeight(.bold)
                                            .foregroundColor(.black)
                                    }
                                    
                                    Spacer()
                                    
                                    Text(landmark.state)
                                        .font(.system(size: 22))
                                        .fontWeight(.bold)
                                        .foregroundColor(.black)
                                }
                                .padding()
                                
                                Text(landmark.imageName)
                                    .foregroundColor(.black)
                                    .padding(.top,20)
                                    .padding(.horizontal)
            
                            
                                    Spacer(minLength: 0)
                                
                                Spacer(minLength: 0)
                                
                                // Button...
                                
                                Button(action: {}) {
                                    
                                    Text("View on Live Map")
                                        .fontWeight(.bold)
                                        .padding(.vertical)
                                        .foregroundColor(.white)
                                        .frame(width: UIScreen.main.bounds.width - 100)
                                        .background(Color.black)
                                        .clipShape(Capsule())
                                }
                                .padding(.bottom,40)
                                .padding(.top)
                            }
                            
                        }
                        .background(Color.white)
                    }
            }
        }

    }
        

解决方法

您应该将该大块中的每个逻辑子部分移到单独的视图中。例如,整个水平滚动视图可以分开并命名为HeaderView,因此,代替了

VStack{
    ScrollView(.horizontal,showsIndicators: false) {
        HStack(spacing: 30){
                // ... other code            
            }
        }
        .padding(.horizontal)
    }
    .padding(.top,60)

你有

    VStack{
        HeaderView()
        // other code

struct HeaderView: View {
   var body: some View {
        ScrollView(.horizontal,showsIndicators: false) {
            
            HStack(spacing: 30){
            
                }
            }
            .padding(.horizontal)
        }
        .padding(.top,60)
   }
}

并以此方式尽可能地打破并合并。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...