Swift 4编程三行情

时间:2017-11-11 05:40:43

标签: swift

是什么意思,当他们说"它与收尾报价的缩进相匹配时?

“对于占用多行的字符串,请使用三个双引号(""")。每个引用行开头的缩进都会被删除,只要它与结束引号的缩进相匹配即可。例如:

让引用=""" 即使左边有空白, 实际的行不缩进。 除了这条线。 双引号(")可以在不转义的情况下出现。

我还有(苹果+橘子)水果片。 """ 使用方括号([])创建数组和字典,并通过在括号中写入索引或键来访问它们的元素。在最后一个元素之后允许使用逗号。“

摘自:Apple Inc.“Swift编程语言(Swift 4)。”iBooks。 https://itunes.apple.com/us/book/the-swift-programming-language-swift-4/id881256329?mt=11

1 个答案:

答案 0 :(得分:4)

这是我能想到解释的三个场景:

此处文字和三重引号与左侧对齐

检查输出,此处文本存储并在每个段落的开头打印,没有空格。

let textSample1 = """
Test 1: Hello how are you welcome to the test
something else is written here
that's enough said
"""

print(textSample1)

此处文字在开头有间距,但三引号对齐左边

检查输出,这里存储文本并在每个段落的开头用空格打印,因为牛栏引用了左边的位置,并且它们考虑了段落中的那些空格。

let textSample2 = """
  Test 2: Hello how are you welcome to the test
  something else is written here
  that's enough said
"""

print(textSample2)

此处文字在开头有间距,三重引号也有间距以匹配文字

检查输出,这里文本存储并在开始时没有空格打印,虽然我们在beginging处放置了空格,这是因为三重引号与文本而不是空格位于同一级别所以它们的空格是忽略。当你想在代码中存储多行文本但希望在其他用途​​中保留一些代码格式时,我发现这很方便。

let textSample3 = """
    Test 3: Hello how are you welcome to the test
    something else is written here
    that's enough said
    """

print(textSample3)

<强>输出:

Test 1: Hello how are you welcome to the test
something else is written here
that's enough said

  Test 2: Hello how are you welcome to the test
  something else is written here
  that's enough said

Test 3: Hello how are you welcome to the test
something else is written here
that's enough said