Skip to content

Instantly share code, notes, and snippets.

@bocato
Last active January 27, 2022 10:24
Show Gist options
  • Save bocato/32829b18094533e44fc84290923ce143 to your computer and use it in GitHub Desktop.
Save bocato/32829b18094533e44fc84290923ce143 to your computer and use it in GitHub Desktop.
public struct AccessibilityIdentifier: ExpressibleByStringLiteral { // This should be inside you UI Module
public let key: String
public init(stringLiteral key: String) {
self.key = key
}
}
public extension View { // This should be inside you UI Module
@ViewBuilder
func setAccessibilityIdentifier(_ identifier: AccessibilityIdentifier) -> some View {
if #available(iOS 14.0, *) {
self.accessibilityIdentifier(identifier.key)
} else {
self
}
}
}
// Then
extension AccessibilityIdentifier {
static let someIdentifier: Self = "i_am_an_identifier"
}
// Then
struct SomeView: View {
var body: some View {
Text("Identified")
.setAccessibilityIdentifier(.someIdentifier)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment