Are Button actions on the MainActor?

I think they are, but I got myself into a situation with this error:

// Inside a SwiftUI View struct...
let cancelled: @MainActor () -> Void

var body: some View {
   ...
   Button(action: self.cancelled) { Text("Cancel") } // Error here

The compiler reports:

Converting function value of type '@MainActor () -> Void' to '() -> Void' loses global actor 'MainActor'

I originally put that attribute on the cancelled property, because in the passed-in closure, I was doing something that gave me an error about how I had to be on the MainActor.

Better fix the passed-in closure, Adding @MainActor may not be the right solution (even when Xcode suggested that).

But the passed-in closure is meant to run on the main actor, and it is, it's just that the complier doesn't seem to understand that.

Seems you expect too much than documented.

It looks like you can work around this using a wrapping closure:

Button(action: { cancelled() }) { Text("Cancel") }
Are Button actions on the MainActor?
 
 
Q