How does SwiftUI find @State properties in views?

I believe SwiftUI doesn't use Mirror, but instead uses type metadata directly to learn the layout of each View-conforming type on demand.

By looking at the metadata directly, SwiftUI gains some efficiency advantages over Mirror. First, SwiftUI can cache layout information in whatever format it finds most convenient. Second, SwiftUI can poke around in View values without necessarily going through Any conversions (which Mirror always performs) and without creating AnyCollection instances and corresponding iterators.

I think SwiftUI also uses type metadata to decompose instances of TupleView.

I'm not sure if the type metadata documentation is currently complete or up to date. I've had trouble in the past with it being neither. But the SwiftUI engineers probably got some help from the Swift language engineers. There are also some open source packages for examining Swift metadata, like Azoy/Echo, that I have found very educational.

Echo's primary author now works for Apple on the Swift standard library.

8 Likes