Quick Tip: Improving AcessibilityIdentifiers in SwiftUI

Introducing simple data modelling approach to improve the way we deal with Accessibility Identifier's in SwiftUI

Eduardo Sanches Bocato
1 min readDec 29, 2021

Introduction

Long story short: I really don't like to see loose strings flying around, so when I saw the way we normally set accessibility identity it really bugged me.

Looking at the Swift API, there is another case that bugged me in the past but now has a solution: Notifications from the NotificationCenter.
For those, it is a good practice to extend Notification.Name, instead of using strings and constants like in the past.

So why not try something similar?

The Improvement

Conclusion

I could have simply extended String, but doing that would expose the identifiers for anything using String, which would not be good. Also, this approach leaves space for introducing new logic, hiding implementation details for something related to the way we define the identifier keys and so on.

--

--

Eduardo Sanches Bocato

iOS Engineer, AI enthusiast, Crossfit/Gymnastics. Currently working as iOS Lead @adidas