Why Certain View Modifiers in Swift 6 Cannot Use the @State Property
Why Certain View Modifiers in Swift 6 Cannot Use the @State Property

SwiftUI in Swift 6 fails with @State in view modifiers due to stricter concurrency & @MainActor. Learn solutions for alignmentGuide, scrollTransition & more.

In Xcode 16, to improve SwiftUI’s performance under Swift 6 mode, Apple made several adjustments to the SwiftUI framework’s APIs to meet stricter concurrency checks. The most notable change is the comprehensive annotation of the View protocol with @MainActor. While these optimizations generally enhance the developer experience in Swift 6 mode, they also introduce some seemingly anomalous compile-time errors in specific scenarios. This article delves into why certain view modifiers cannot directly use @State properties and provides corresponding solutions.