segmented picker style causes runtime warning

On OSX, using a segmented picker style causes the following warning to be emitted (when a different selection is made) : "Publishing changes from within view updates is not allowed, this will cause undefined behavior." The warning is not emitted using the 'Preview Canvas' only when running the app.

Changing the picker style to automatic also fixes it, but the segmented style is used extensively.

Tested using XCode 26.4.1 on MacOS 26.3.1

--- view ---

import SwiftUI
internal import Combine

enum Mode : String {
    case one, two, three
}

class MyObject : ObservableObject {
    @Published var mode : Mode = .one
}

struct ContentView: View {
    @StateObject var obj = MyObject()

    var body: some View {
        VStack {
            Picker("Mode",selection: $obj.mode) {
                Text("One").tag(Mode.one)
                Text("Two").tag(Mode.two)
                Text("Three").tag(Mode.three)
            }
            .pickerStyle(.segmented)
        }
        .padding()
    }
}

#Preview {
    ContentView()
}

--- app ---

import SwiftUI

@main
struct SwiftUIBugApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
Answered by DTS Engineer in 885313022

You are correct, changed that to macOS and I see the warning: Publishing changes from within view updates is not allowed, this will cause undefined behavior.

Please file a bug for that. Once you open the bug report, please post the FB number here for my reference.

If you have any questions about filing a bug report, take a look at Bug Reporting: How and Why?

Albert
  Worldwide Developer Relations.

Thanks so much for this very interesting post. I used the latest beta of Xcode and I do not see this warning with Version 26.5 beta 2.

I believe, but haven’t seen this before, maybe a known issue in SwiftUI on macOS. It occurs because of how SwiftUI bridges AppKit's NSSegmentedControl to the .segmented picker style. That's why changing it to .automatic does not provide you this warning. But please do try the latest beta.

Let me know if you can try that Xcode Version.

Albert
  Worldwide Developer Relations.

Thanks, but I am a bit confused. Your screenshot is of an iOS app, not a MacOS one.

I can't really run the beta, because I need to build for production. Did you verify that it occurs under the latest XCode production release?

Accepted Answer

You are correct, changed that to macOS and I see the warning: Publishing changes from within view updates is not allowed, this will cause undefined behavior.

Please file a bug for that. Once you open the bug report, please post the FB number here for my reference.

If you have any questions about filing a bug report, take a look at Bug Reporting: How and Why?

Albert
  Worldwide Developer Relations.

I filed FB22571981

Thanks for doing that, the bug got routed to the correct team. They'll communicate using the Feedback Assistant.

We're unable to share any updates on specific reports on the forums.

For more details on when you'll see updates to your report, please see What to expect after submission.

Albert
  Worldwide Developer Relations.

segmented picker style causes runtime warning
 
 
Q