Persistent 'buildExpression unavailable' error in ContentView with switch/AppState

Hi everyone,

I'm encountering a persistent build error in a SwiftUI iOS app and I'm running out of ideas.

Setup: My ContentView uses two @EnvironmentObjects (GameViewModel, SettingsStore). The GameViewModel has an AppState enum (.welcome, .setup, .game). The ContentView body uses a switch viewModel.currentAppState (wrapped in a Group) to display one of three different views (WelcomeView, SetupView, GameView). Navigation between states is triggered by changing viewModel.currentAppState within withAnimation blocks in the respective subviews.

Problem: I consistently get the build error 'buildExpression' is unavailable: this expression does not conform to 'View' pointing to the lines inside the .setup and .game cases of the switch statement in ContentView.

Code (ContentView.swift - Simplified Test Version that STILL fails):

// Zweck: Steuert die Hauptnavigation basierend auf AppState
// KORRIGIERTE VERSION OHNE .animation(...) am Ende
import SwiftUI
struct ContentView: View {
// Zugriff auf das ViewModel, um den AppState zu lesen
@EnvironmentObject var viewModel: GameViewModel
// SettingsStore wird von untergeordneten Views benötigt
@EnvironmentObject var settingsStore: SettingsStore
var body: some View {
// Optional: Group um das switch-Statement, kann manchmal helfen (kannst du auch weglassen)
Group {
// Wechsle die Ansicht basierend auf viewModel.currentAppState
switch viewModel.currentAppState {
case .welcome:
WelcomeView()
// EnvironmentObjects an WelcomeView übergeben
.environmentObject(viewModel)
.environmentObject(settingsStore)
// Übergangsanimation
.transition(.opacity)
case .setup:
SetupView()
// EnvironmentObjects an SetupView übergeben
.environmentObject(viewModel)
.environmentObject(settingsStore)
// Übergangsanimation
.transition(.asymmetric(insertion: .move(edge: .trailing), removal: .move(edge: .leading)))
case .game:
GameView()
// EnvironmentObjects an GameView übergeben
.environmentObject(viewModel)
.environmentObject(settingsStore)
// Übergangsanimation
.transition(.asymmetric(insertion: .move(edge: .trailing), removal: .move(edge: .leading)))
}
} // Ende der optionalen Group
// !!! WICHTIG: KEIN .animation(...) Modifier hier !!!
}
}
// Vorschau
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
// Erstelle Instanzen für die Vorschau
let vmWelcome = GameViewModel()
vmWelcome.currentAppState = .welcome
let vmSetup = GameViewModel()
vmSetup.currentAppState = .setup
let vmGame = GameViewModel()
vmGame.currentAppState = .game
vmGame.currentCard = Card.defaultCards.first
let settings = SettingsStore()
// Zeige verschiedene Zustände in der Vorschau an
Group {
ContentView()
.environmentObject(vmWelcome)
.environmentObject(settings)
.previewDisplayName("Welcome State")
ContentView()
.environmentObject(vmSetup)
.environmentObject(settings)
.previewDisplayName("Setup State")
ContentView()
.environmentObject(vmGame)
.environmentObject(settings)
.previewDisplayName("Game State")
}
}
}

Troubleshooting Steps Taken (No Success):

  • Ensured correct placement of .environmentObject modifiers on subviews within the switch.
  • Removed a previous .animation() modifier applied directly to the switch.
  • Ensured state changes triggering transitions are wrapped in withAnimation.
  • Wrapped the switch in a Group.
  • Multiple "Clean Build Folder".
  • Deleted entire Derived Data folder (with Xcode closed).
  • Restarted Xcode and the Mac multiple times.
  • Deleted and recreated ContentView.swift with the code above.
  • Crucially: The errors persist even when replacing WelcomeView(), - - - --- SetupView(), and GameView() with simple Text("...") views inside the switch cases (as shown in the code snippet above).

Environment: Xcode Version: newest macOS Version: newest

Question: Does anyone have any idea why the compiler would still fail to type-check this switch structure, even when the views inside are simplified to basic Text? What else could I try to diagnose or fix this? Could it be related to the subviews (SetupView/GameView) potentially having their own NavigationView or complexity, even when replaced by Text in the failing ContentView?

Thanks for any suggestions!

Problem: I consistently get the build error 'buildExpression' is unavailable: this expression does not conform to 'View' pointing to the lines inside the .setup and .game cases of the switch statement in ContentView.

No problem with WelcomeView ?

Do you still get the problem if you replace

.transition(.asymmetric(insertion: .move(edge: .trailing), removal: .move(edge: .leading)))

by

.transition(.opacity)

in SetupView ?

Could you also show how you declare the environment var in

struct TheApp: App {
}

You don't need the Groups unless you're doing something with them, like adding modifiers that would apply to all of the contents. (Think of adding a .bold() modifier to the Group to apply bold rather than having to add three separate instances of .bold().) That's just a guess, though.

Second guess, is it because you're creating a preview with three separate instances of the ContentView() and Xcode is getting confused? Can you try three separate previews?

Persistent 'buildExpression unavailable' error in ContentView with switch/AppState
 
 
Q