Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Text("") and Text(verbatim: "") initializers in SwiftUI

Tags:

swift

swiftui

I've been following Apple's SwiftUI tutorials. Along the way, I've often used the Text object with the following initializer:

/// Creates an instance that displays `content` verbatim.
public init<S>(_ content: S) where S : StringProtocol

Now, in the fifth tutorial of the series, I've encountered the following usage of Text:

Text(verbatim: "")

The description in the interface is the same as for the other initializer:

/// Creates an instance that displays `content` verbatim.
public init(verbatim content: String)

Question

What's the two initializers for and how are they different / when would I use which?

like image 364
LinusGeffarth Avatar asked Jun 08 '19 10:06

LinusGeffarth


1 Answers

Text(verbatim: ) returns text as it is - hence the verbatim argument name.

Text(:_) checks if the argument is a localized key.

If it is, it returns the corresponding localized string.

It it isn't, it will print the text verbatim.

like image 58
Matteo Pacini Avatar answered Sep 28 '22 01:09

Matteo Pacini