The following SwiftUI app snippet causes a 100% reproducible crash on macOS.
import SwiftUI
struct Rule: Identifiable {
let id = UUID()
var text = ""
}
@MainActor @Observable final class Policy {
var rules: [Rule] = [
.init(text: "Row 1"),
.init(text: "Row 2")
]
}
@main struct MyApp: App {
@State private var policy = Policy()
var body: some Scene {
WindowGroup {
List {
ForEach($policy.rules) { $rule in
HStack {
TextField("Value", text: $rule.text)
Spacer()
Button("Remove") {
policy.rules.removeAll { $0.id == rule.id }
}
}
}
}
.padding()
}
}
}
I first encountered this on macOS 27, but have also reproduced it on macOS 26.6.2. In both cases, the app was compiled with Xcode 27 from a standard “New Project” template.
(Default build settings: Swift 5, Main Actor Isolation, Approachable Concurrency enabled.)
Steps to reproduce:
- Open Xcode and select “New Project… > App”.
- Copy and paste the snippet above into a Swift file.
- Build and run.
- Click one of the two text fields to give it focus.
- Click the
Removebutton for that same row.
The application crashes with an access violation.
My assumption is that the row is removed from the array while the TextField still holds a Binding to it. When the text field subsequently loses focus, it attempts to write through a binding whose underlying element no longer exists.
There are ways to work around this, but I’d be interested in guidance from the SwiftUI or AppKit team on the intended way to handle this situation.
The same kind of issue can arise with custom bindings, but it is particularly difficult to guard against when framework controls perform reactivate actions, like when it loses focuses.