SwiftUI Text init with verbatim parameter


Greetings, traveler!

When diving into SwiftUI development, one of the fundamental components you’ll frequently interact with is Text. SwiftUI’s Text initializer offers several ways to initialize text content. Today we will explore two of them: with the verbatim parameter and without it.

Without verbatim

The standard initializer for Text in SwiftUI looks like this:

Text("Hello, World!")

When you pass a string literal to Text, SwiftUI treats it as a LocalizedStringKey. This enables automatic localization and string interpolation handling at compile time.

This means if your app supports multiple languages, the text can be translated based on the user’s language preference.

With verbatim

Here’s how you use the verbatim initializer:

Text(verbatim: "Hello, World!")

Using the verbatim parameter is good way to treat the text exactly as written, with no localization applied. This becomes relevant when displaying server responses, log messages, identifiers, or any dynamic content that should remain untouched by the localization system. In these cases, explicit intent matters.

Example

struct GreetingsView: View {
    var body: some View {
        VStack {
            Text("Greetings, traveler!") // "Saludos, viajero!" in Spanish
            Text(verbatim: "Greetings, traveler!") // No localization
        }
    }
}

Using a String variable

There is also a subtle distinction when working with stored String values. A string literal passed inline behaves differently from a String variable passed to Text. That nuance can affect how text is resolved and is worth understanding before assumptions leak into production code.

let title = "Hello"
Text(title) // No localization

Conclusion

Small API differences often shape architecture decisions over time. Text is no exception. Knowing which initializer you are calling keeps localization predictable and avoids hard-to-trace issues later in the lifecycle of an app.