· 1 min read

SwiftUI Preview crashes with List ForEach subviews on macOS

When using a custom subview inside List ForEach for macOS, Xcode Preview crashes. Add AnyView(EmptyView()) as a workaround.

When using a custom subview inside List ForEach for macOS, Xcode Preview crashes. Add AnyView(EmptyView()) as a workaround.

I ran into a SwiftUI Preview crash when building a macOS app. The preview works fine for iOS, but crashes for macOS targets.

The crashing code

This minimal example reproduces the crash:

struct ContentView: View {
    var body: some View {
        List {
            ForEach(0..<3, id: \.self) { _ in
                SubView() // Crashes preview
            }
        }
    }
}

struct SubView: View {
    var body: some View {
        Text("Hello, World!")
    }
}

The preview crashes with PREVIEW UPDATE ERROR: GroupRecordingError. The crash only happens when using a custom subview inside List { ForEach }. Inline views like Text("Hello") work fine. Using the “Use Legacy Previews Execution” option did not help.

The workaround

Adding AnyView(EmptyView()) prevents the crash:

struct ContentView: View {
    var body: some View {
        List {
            ForEach(0..<3, id: \.self) { _ in
#if DEBUG
                AnyView(EmptyView())
#endif
                SubView()  // Works
            }
        }
    }
}

Environment

  • macOS 26.2
  • Xcode 26.2
Back to Blog

Related posts

View all posts »