SwiftUI 中 Text("") 和 Text(verbatim: "") 初始值设定项之间的区别 [英] Difference between Text("") and Text(verbatim: "") initializers in SwiftUI
问题描述
我一直在关注 Apple 的 SwiftUI 教程.在此过程中,我经常使用带有以下初始化程序的 Text
对象:
///创建一个实例,逐字显示 `content`.public init<S>(_ content: S) where S : StringProtocol
现在,在系列的第五篇教程中,我遇到过Text
的以下用法:
文本(逐字:")
接口中的描述与其他初始化器相同:
///创建一个实例,逐字显示 `content`.公共初始化(逐字内容:字符串)
问题
这两个初始值设定项的用途是什么?它们有何不同/我何时使用哪个?
Text(verbatim: )
返回文本原样 - 因此是逐字参数名称.>
Text(:_)
检查参数是否为本地化键.
如果是,则返回相应的本地化字符串.
不是,它会逐字打印文本.
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?
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.
这篇关于SwiftUI 中 Text("") 和 Text(verbatim: "") 初始值设定项之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!