0

Ever since SwiftUI 2 released, the compiler and debugger have just become awful. Whenever I add another view (that is a custom struct or even just a VStack containing a Text() element) to the ZStack in 'viewThatCanContainOtherViews()', the code refuses to compile. Each generally also contains some Stacks or Buttons or whatever. I have a feeling the ViewBuilder is just too complex but I don't understand if this isn't how you do it, how do other apps do it? The code just does not compile and I'm sure that it's not a syntactical error somewhere because it compiles (albeit extremely slowly) unless I add another view in which case I get an error.

The generalized code format is what's seen below:

struct ContentView: View {
var body: some View {
    GeometryReader { proxy in
        ZStack{
            VStack {
                if bool1{
                    if bool2 {
                        view1()
                    } else if bool3 {
                        view1() //different initialization
                    }
                } else {
                    VStack {
                        Spacer()
                        HStack {
                            Spacer()
                            if bool4 {
                                view2()
                            } else if bool5 {
                                view2() // different initialization
                            }
                            Spacer()
                        }
                        Spacer()
                    }
                }
            }.onAppear {
                //code that is run on appear
            }
            .onReceive(variable) { input in
                //code that is run sometimes
            }
            VStack{
                HStack{
                    Spacer()
                    VStack {
                        if bool6 {
                            if bool7 {
                                view3()
                            } else if bool8 {
                                view3() //different initialization
                            }
                        } else {
                            if bool9 {
                                view4()
                            } else if bool10 {
                                view4() //different initialization
                            }
                        }
                    }
                }
                Spacer()
            }
            if bool11 {
                VStack {
                    Spacer()
                    HStack {
                        Spacer()
                        view5()
                        Spacer()
                    }
                    Spacer()
                }
            }
            VStack {
                Spacer()
                HStack{
                    Spacer()
                    if bool12 {
                        if bool13 {
                            view6()
                            //Instead of passing in searchedText, we need to pass in the mapView...idk how though
                        } else if bool14 {
                            view6() //different initialization
                        }
                    }
                    Spacer()
                }
                Spacer()
            }
            Group {
                VStack {
                    if bool15 {
                        viewThatCanContainOtherViews() {
                            ZStack {
                                //If I add views here it doesn't compile
                                Group {
                                    if bool15 {
                                        VStack {
                                            view7()
                                        }
                                    }
                                    if bool16 {
                                        view8()
                                    }
                                }
                                Group {
                                    VStack {
                                        if bool17 {
                                            view9()
                                        }
                                        if bool18 {
                                            view10()
                                            view11()
                                        }
                                        Spacer()
                                    }
                                }
                                Group {
                                    VStack {
                                        if bool19 {
                                            view12()
                                        }
                                        Spacer()
                                    }
                                }
                            }
                        }
                    }
                    else if bool20 {
                        viewThatCanContainOtherViews {
                            ZStack {
                                Group {
                                    if bool21 {
                                        VStack {
                                            view7()
                                        }
                                    }
                                    if bool22 {
                                        view8()
                                    }
                                }
                                Group {
                                    VStack {
                                        if bool23 {
                                            view9()
                                        }
                                        
                                        if bool24 {
                                            view10()
                                            view11()
                                        }
                                        Spacer()
                                    }
                                }
                                Group {
                                    VStack {
                                        if bool25 {
                                            view12()
                                        }
                                        Spacer()
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if bool26 {
                if bool27 {
                    view13()
                } else if bool28 {
                    view13() //different initialization
                }
            }
            Group {
                if bool28 {
                    if bool29 {
                        view14()
                            .onDisappear(perform: { //do stuff
                            })
                    } else if bool30 {
                        view14() // different initializer
                            .onDisappear(perform: { //do stuff
                            })
                    }
                }
                if bool31 {
                    if bool32 {
                        view15()
                            .onDisappear(perform: { //do stuff
                            })
                    } else if bool33 {
                        view15()
                            .onDisappear(perform: { //do stuff
                            })
                    }
                }
            }
            
        }
    }
}
}
nickcoding
  • 305
  • 8
  • 35
  • 2
    Comment out big chunks to eliminate what part is causing the compiling issue. Usually it is not passing in all the parameters to a View, but it should be relatively quick to debug. – George Oct 27 '20 at 00:23
  • 2
    Because Swift infers types, there's a limit to how complex an expression could be before the compiler bails (even if it's syntactically correct). You're definitely not following the best practices by making your view so big. Break it into smaller subviews – New Dev Oct 27 '20 at 00:44
  • I saw some posts on here that showed breaking it into subviews...correct me if I'm wrong but it seems like to make a subview, I'd just take for example, all the views inside that ZStack inside viewThatCanContainOtherViews() and just make it the body of some new struct. Then in ContentView I'd just initialize that struct. I tried to do that but it didn't work--it seems like it compiles exactly the same. Is there something else meant by subviews? @NewDev – nickcoding Oct 27 '20 at 00:55
  • Also, I am passing around a metric ton of parameters in all these views because this is a rather large application, could that be what's causing the error @George_E – nickcoding Oct 27 '20 at 00:56
  • 1
    @nickcoding It’s not the number of parameters, it’s not passing the parameters it expects. Just comment out whole `HStack`s for example, and if it then compiles, the problem is somewhere in there. If you comment everything else except the problem, the compiler will then tell you the _real_ issue – George Oct 27 '20 at 01:42
  • @George_E Oh I've done that, that's why it's weird. It'll compile but then I'll add one view like a Text() element or something and it won't compile, meaning the issue is the additional view, not the parameters being passed in incorrectly... – nickcoding Oct 27 '20 at 01:46
  • 1
    @nickcoding It should be `Text(“”)` not `Text()`, but anyway... If the problem is just adding more views, then as @NewDev said you need to separate into separate views. You are correct in saying you need separate structs conforming to View. After splitting it enough the compiler will eventually tell you the issue – George Oct 27 '20 at 01:55
  • Do you have any recommendations about which parts I should put into separate structs? How do I determine that? What's best practice? Will this cause the compile time to go down? @George_E – nickcoding Oct 27 '20 at 01:57
  • 2
    @nickcoding, a general rule of thumb is to try to make a view to have a single purpose. For example, you have multiple `VStack`s inside a `ZStack` - might make sense to separate... – New Dev Oct 27 '20 at 02:32
  • Separating them worked--thank you! – nickcoding Oct 27 '20 at 23:11

0 Answers0