# Make your SwiftUI view modifiers safer

Is something wrong with this code?

```Swift
struct ContentView: View {
    var body: some View {
        Text("Hello, world!")
            padding()
    }
}
```

Look closer 🧐

```Swift
struct ContentView: View {
    var body: some View {
        Text("Hello, world!")
            padding() //<-- missing the "."
    }
}
```

Typo alert ! 😲

The code will compile, but it will crash the app! Why?

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 other than ...

![constant-vigilance.jpeg](https://cdn.hashnode.com/res/hashnode/image/upload/v1653592540486/b0U4xa-eZ.jpeg align="center")

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

According to the [Swift documentation](https://docs.swift.org/swift-book/ReferenceManual/Attributes.html):

> Apply this attribute to a top-level function, instance method, or class or static method to trigger warnings when that function or method is used without a preceding qualifier, such as a module name, type name, or instance variable or constant. Use this attribute to help discourage ambiguity between functions with the same name that are accessible from the same scope.

To better illustrate the use I am taking [Apple's example](https://developer.apple.com/documentation/swiftui/viewmodifier) of a custom modifier that you can use to create blue caption text surrounded by a rounded rectangle:

```Swift
struct BorderedCaption: ViewModifier {
    func body(content: Content) -> some View {
        content
            .font(.caption2)
            .padding(10)
            .overlay(
                RoundedRectangle(cornerRadius: 15)
                    .stroke(lineWidth: 1)
            )
            .foregroundColor(Color.blue)
    }
}

extension View {
    @warn_unqualified_access //<-- produces a warning in Xcode if called without "." 
    func borderedCaption() -> some View {
        modifier(BorderedCaption())
    }
}
```

By adding `@warn_unqualified_access` to the function declaration of the `View` extension, Xcode is able to produce a warning whenever the function is called without the dot.

![warning.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653589883959/aLgVOec1d.png align="left")

This article is greatly inspired by Federico Zanetello's [article](https://www.fivestars.blog/articles/warn_unqualified_access/) which taught me about the existence and usefulness of `@warn_unqualified_access`.
