Preview crashes when using ForEach with a Binding to an array and generics

The following repro case results in a previews crash on Xcode 26 beta 3 (report attached). FB18762054

import SwiftUI

final class MyItem: Identifiable, Labelled {
    var label: String
    
    init(_ label: String) {
        self.label = label
    }
}

protocol Labelled {
    var label: String { get }
}

struct HelloView: View {
    let label: String
    
    var body: some View {
        Text(label)
    }
}

struct ListView<Element: Labelled & Identifiable>: View {
    @Binding var elements: [Element]
    
    var body: some View {
        List {
            ForEach($elements, id: \.id) { $element in
                HelloView(label: element.label) // crash
                
                // Replacing the above with a predefined view works correctly
                // Text(element.label)
            }
        }
    }
}

struct ForEachBindingRepro: View {
    @State var elements: [MyItem] = [
        MyItem("hello"),
        MyItem("world"),
    ]
    
    var body: some View {
        ListView(elements: $elements)
    }
}

#Preview("ForEachBindingRepro") {
    ForEachBindingRepro()
}

Simpler repro without generics.

import SwiftUI

struct MyItem: Identifiable {
    var id = UUID()
    var label: String
    
    init(_ label: String) {
        self.label = label
    }
}

struct HelloView: View {
    let label: String
    
    var body: some View {
        Text(label)
    }
}

struct ForEachBindingRepro: View {
    @State var elements: [MyItem] = [
        MyItem("hello"),
        MyItem("world"),
    ]
    
    var body: some View {
        List {
            ForEach($elements, id: \.id) { $element in
                HelloView(label: element.label) // crash
                
                // Works ok:
                // Text(element.label)
                
            }
        }
    }
}

#Preview("ForEachBindingRepro") {
    ForEachBindingRepro()
}

It also crashes in Xcode 16.4 because what you're doing isn't valid.

In your second example, line 28 is wrong.

HelloView does not have any Bindings in it; it just takes a label parameter, so line 29 passes in element.label. Correct.

Line 28 is treating your elements State var as though it's a binding, by adding the $ in front. And you're also adding the $ to the element part towards the end of the line.

elements is a State var and element is a var whose label property is being passed into a view that does not have a Binding. This is why Preview crashes.

It will compile if line 13 is this instead: @Binding var label: String, but based on what you're doing with label inside HelloView, i.e. you're not changing it, there's no need for it to be a binding var.

The following repro case results in a previews crash on Xcode 26 beta 3 (report attached).

Thanks for reporting this. I believe the feedback you meant to reference was FB18792113 which I've made sure has reached the right team for the initial triaging of your issue. If we need more information we will reach out to you via the feedback mechanism.

@Developer Tools Engineer thanks. The FB you linked is the right one.

@darkpaw Here's a simpler repro without bindings that still crashes.

import SwiftUI

struct MyItem: Identifiable {
    var id = UUID()
    var label: String
}

struct HelloView: View {
    let element: MyItem
    
    var body: some View {
        Text(element.label)
    }
}

struct ForEachSubView: View {
    @State var elements: [MyItem] = [
        MyItem(label: "hello"),
        MyItem(label: "world"),
    ]
    
    var body: some View {
        List {
            ForEach(elements, id: \.id) { element in
                HelloView(element: element) // crash
                
                // Works ok:
                // Text(element.label)
                
            }
        }
    }
}

#Preview("ForEachSubViewRepro") {
    ForEachSubView()
}

Seems SwiftUI is internally invoking an "OutlineList" and then crashing when trying to walk a tree, despite the fact that MyItem has no children and no children keypath is specified in the List constructor.

Thread 0 Crashed:
0   libswiftCore.dylib            	       0x1a9b458f0 _assertionFailure(_:_:file:line:flags:) + 176
1   SwiftUI                       	       0x1cab7e9c8 ViewListTree.visitItem(_:force:) + 5232
2   SwiftUI                       	       0x1cab8948c OutlineListCoordinator.outlineView(_:child:ofItem:) + 980
3   SwiftUI                       	       0x1cab895cc @objc OutlineListCoordinator.outlineView(_:child:ofItem:) + 128
4   AppKit                        	       0x19a88a694 loadItemEntryLazyInfoIfNecessary + 340
5   AppKit                        	       0x19b14ae50 -[NSOutlineView itemAtRow:] + 48
6   AppKit                        	       0x19b14e63c -[NSOutlineView _delegate_isGroupRow:] + 48
7   AppKit                        	       0x19b642f14 -[NSTableRowData _cacheGroupRowIndexesWithinRange:] + 92
8   AppKit                        	       0x19b642e74 -[NSTableRowData isGroupRowIndex:] + 144
9   AppKit                        	       0x19b0d636c -[NSTableView _spacingPrecedingRow:] + 44
10  AppKit                        	       0x19b5747c4 -[NSTableRowHeightData _cacheRowSpansInRange:heightProvider:] + 612
11  AppKit                        	       0x19b572d30 -[NSTableRowHeightData _cacheRowSpansInRange:] + 100
12  Foundation                    	       0x197c1b3b8 __NSINDEXSET_IS_CALLING_OUT_TO_A_RANGE_BLOCK__ + 24
13  Foundation                    	       0x1985faee8 __NSIndexSetEnumerateBitfield + 340
14  AppKit                        	       0x19b574a04 -[NSTableRowHeightData _estimatedSpanForRow:cacheIfNecessary:] + 256
15  AppKit                        	       0x19b571ee4 -[NSTableRowHeightData _estimatedSpanForRowsInRange:] + 48
16  AppKit                        	       0x19b572018 -[NSTableRowHeightData computeTotalRowsSpan] + 68
17  AppKit                        	       0x19b572224 -[NSTableRowHeightData _estimatedRowAtOffset:] + 36
18  AppKit                        	       0x19b2df190 -[NSTableView rowAtPoint:] + 204
19  AppKit                        	       0x19b63eb60 -[NSTableRowData _keepTopRowStableAtLeastOnce:andDoWorkUntilDone:] + 192
20  AppKit                        	       0x19a895474 -[NSTableRowData _updateVisibleViewsBasedOnUpdateItems] + 1340
21  AppKit                        	       0x19a8897cc -[NSTableRowData endUpdates] + 348
22  AppKit                        	       0x19b2dfc34 -[NSTableView _endUpdateWithTile:] + 108
23  AppKit                        	       0x19a8e2c4c -[NSTableView endUpdates] + 28
24  SwiftUI                       	       0x1cab6dc20 OutlineListCoordinator.diffRows(of:to:) + 112

Your new example doesn't cause a crash in Xcode 26 beta 3 or Xcode 16.4 on my M1 Max Mac Studio with macOS 15.5.

I wonder if it's something else. Are you on macOS 26 beta?

Yes macOS 26.0 Beta (25A5306g)

Preview crashes when using ForEach with a Binding to an array and generics
 
 
Q