WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

How to make SwiftUI modifiers safer to use with @warn_unqualified_access

Paul Hudson    @twostraws   

Updated for Xcode 14.2

Every SwiftUI makes the same mistake at some point, and sadly it’s something you’ll do more than once: rather than writing .someModifier() you write someModifier(), and it causes your app to completely freeze or crash with EXC_BAD_ACCESS.

First I’ll show you the how to solve the problem, then I’ll explain what the underlying problem is. The solution is to use Swift’s @warn_unqualified_access attribute, which means you can’t access properties or methods without using a variable name or similar.

For example, if we had a titleStyle() method that added a bunch of modifiers to a view to make it matching a custom theme, we’d use @warn_unqualified_access before the method signature, like this:

extension View {
    @warn_unqualified_access
    func titleStyle() -> some View {
        self
            .font(.largeTitle)
            .fontWeight(.black)
            .padding()
            .background(.blue)
            .foregroundColor(.white)
            .cornerRadius(10)
    }
}

When we use that, we’d write the same SwiftUI code we always did:

struct ContentView: View {
    var body: some View {
        Text("Welcome")
            .titleStyle()
    }
}

That works exactly as expected – the extra attribute hasn’t made the method behave differently at all in normal circumstances.

However, take a look at this code:

struct ContentView: View {
    var body: some View {
        Text("Welcome")
            titleStyle()
    }
}

That’s subtly different: I removed the dot before titleStyle(), which is an unqualified access – I haven’t said where I’m calling titleStyle(), so SwiftUI assumes I’m actually calling it on ContentView. This means it’s actually calling self.padding(), so we have an infinitely recursive view and ultimately a crash.

Sadly, SwiftUI doesn’t use @warn_unqualified_access with its own modifiers, but you can at least add it for the custom ones you build.

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Similar solutions…

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 5.0/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.