How to add multiple `some View`s in a `View` struct?

So I've been struggling with an error which shows that the code is too long to process, so I decided to split a View struct into multiple some Views.

That hasn't worked well as it shows even more errors.

Here is some of the code:

struct ContentView: View {
  var timer2 = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
  let bg = LinearGradient(
    gradient: Gradient(colors: [Color.pink, Color.yellow]),
    startPoint: .top, endPoint: .bottom)
  if gameover {
    var body: some View {
      ZStack { // 1
          bg.ignoresSafeArea()
          VStack {
            gameoverscreen()
          }
      }
    }
  } else if (page == "main") {
    var body: some View {
      ZStack { // 1
          bg.ignoresSafeArea()
          VStack {
            mainpage()
          }
      }
    }
  } else if (page == "multiplication game") {
    var body: some View {
      ZStack { // 1
          bg.ignoresSafeArea()
          VStack {
            multiplicationgame()
          }
      }
    }
  } else if (page == "addition game") {
    var body: some View {
      ZStack { // 1
          bg.ignoresSafeArea()
          VStack {
            additiongame()
          }
      }
    }
}

On the first line, it shows the error: Type 'ContentView' does not conform to protocol 'View'. On the if gameover { line, it shows the error: Expected declaration

Is there a way I can fix it, and if there is, how? Thanks

I don't really understand the problem you had initially, but new code should be restructured.

Don't have     var body: some View { in an if statement, but have the if after.

struct ContentView: View {
    var timer2 = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
    let bg = LinearGradient(
        gradient: Gradient(colors: [Color.pink, Color.yellow]),
        startPoint: .top, endPoint: .bottom)
    
    var body: some View {
        if gameover {
            ZStack { // 1
                bg.ignoresSafeArea()
                VStack {
                    gameoverscreen()
                }
            }
        } else if (page == "main") {
            ZStack { // 1
                bg.ignoresSafeArea()
                VStack {
                    mainpage()
                }
            }
        } else if (page == "multiplication game") {
            ZStack { // 1
                bg.ignoresSafeArea()
                VStack {
                    multiplicationgame()
                }
            }
        } else if (page == "addition game") {
            ZStack { // 1
                bg.ignoresSafeArea()
                VStack {
                    additiongame()
                }
            }
        }
    }
}

body could be condensed as:

    var body: some View {
        bg.ignoresSafeArea()
        ZStack { // 1
            VStack {
                if gameover {
                    gameoverscreen()
                } else if page == "main" {
                    mainpage()
                } else if page == "multiplication game" {
                    multiplicationgame()
                } else if page == "addition game" {
                    additiongame()
                } else { EmptyView() }       // Not strictly needed, but safer to handle all cases explicitly.
            }
        }
    }

Notes:

  • in Swift, struct names should be CamelCase : GameOverScreen vs gameoverscreen or MultiplicationGame vs multiplicationgame
  • no need to enclose value to test in parenthesis. It is enough to write:
if page == "main" {

Note: I assumed that GameOverScreen, MultiplicationGame… are declared as:

struct MultiplicationGame: View {
  var body: some View {
      Text("MultiplicationGame") // Of course, more content
  }
}

I tested the exact code I posted. An it works.

So the problem is elsewhere, maybe in one of the struct GameOverScreen, MultiplicationGame…

Please provide more code and exact location of error.

How to add multiple `some View`s in a `View` struct?
 
 
Q