Bug Report: SwiftUI @State Array Assignment Fails to Trigger UI Updates. Presumably when lengths of the old and new arrays are the same.

Environment:

— Xcode Version: [Current version]

— iOS Version: [Target iOS version]

— Swift Version: 6.0.3

— Platform: iOS

Summary:

Direct assignment of new arrays to @State properties sometimes fails to trigger UI updates, even when the arrays contain different data. The assignment appears to succeed in code but the UI continues to display stale data. Presumably when lengths of both arrays are the same. Assigning first empty array and then the new array fixed the issue.

Expected Behavior:

When assigning a new array to a @State property (self.stateArray = newArray), SwiftUI should detect the change and update the UI to reflect the new data.

Actual Behavior:

The assignment self.stateArray = newArray executes without error, but the UI continues to display data from the previous array. Debugging shows that self._stateArray (the underlying property wrapper) retains the old data despite the assignment. Minimal Reproduction Case:

  struct ContentView: View {
      @State private var items: [String] = ["Old Item"]
      var body: some View {
          VStack {
              ForEach(items, id: \.self) { item in
                  Text(item)
              }
              Button("Update Items") {
                  // This assignment may not trigger UI update
                  self.items = ["New Item", "Another Item"]
                  // Workaround: Clear first, then assign
                  // self.items = []
                  // self.items = ["New Item", "Another Item"]
              }
          }
      }
  }

Workaround:

Force the state update by clearing the array before assignment:

self.items = [] // Clear first
self.items = newArray // Then assign new data

Additional Context:

— This issue was discovered in a production app where item data loaded from cache wasn't updating the UI.

— The same data loading pattern worked in one view (which is modal and doesn't reload data) but failed in another (which needs to be refreshed).

— Console logs showed fresh data was loaded but UI displayed stale data.

— Debugger showed self._items instead of self.items, which might suggest property wrapper issues.

Impact:

This causes significant user experience issues where the UI doesn't reflect the actual application state, leading to confusion and apparent data inconsistency.

Request:

Please investigate why direct @State array assignment sometimes fails and consider fixing the underlying cause, or at minimum document this behavior and recommended workarounds.

Bug Report: SwiftUI @State Array Assignment Fails to Trigger UI Updates. Presumably when lengths of the old and new arrays are the same.
 
 
Q