Wednesday, June 1, 2022

Safer SwiftUI View Modifiers

Marco Eidinger:

Despite the missing qualifier, the code compiles because padding() gets treated as self.padding(). During runtime, that leads to an infinitely recursive View => stack overflow 💥

I did not find a technique to detect the mistake of a missing dot when using standard SwiftUI view modifiers[…]

But for custom view modifiers there is a simple way. We can use the Swift attribute @warn_unqualified_access to warn us in Xcode :)

Update (2022-06-02): See also: Helge Heß.

Comments RSS · Twitter

Leave a Comment