有人可以告诉我如何用以下方式用“双引号”打印内容吗?

“双引号”

最佳答案

要在字符串中插入双引号之前有一个反斜杠:

let sentence = "They said \"It's okay\", didn't they?"

现在sentence是:



它被称为“转义”字符:您使用的是字面值,不会被解释。

使用Swift 4,您也可以选择对不需要转义的文字文本使用"""分隔符:

let sentence = """
They said "It's okay", didn't they?
Yes, "okay" is what they said.
"""

这给出:



使用Swift 5,您可以使用增强的定界符:



您的字符串现在可以表示为:
let sentence = #"They said "It's okay", didn't they?"#

而且,如果要将变量添加到字符串中,还应在反斜杠后添加#:
let sentence = #"My "homepage" is \#(url)"#

10-08 14:30