【问题标题】:Swift 4 Programming Triple QuotesSwift 4 编程三引号
【发布时间】:2017-11-11 05:40:43
【问题描述】:

当他们说“它与结束引号的缩进匹配?”时,这是什么意思?

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

让引号 = """ 即使左边有空格, 实际行没有缩进。 除了这条线。 双引号 (") 可以出现而不被转义。

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

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

【问题讨论】:

  • 我强烈推荐打开 Xcode Playground 并玩弄这些东西......你可能会惊讶于你的发现;)

标签: swift


【解决方案1】:

我可以想到以下三种情况来解释这一点:

这里的文字和三引号左对齐

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

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)

这里的文本在开头有间距,三引号也有间距以匹配文本

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-30
    • 1970-01-01
    • 1970-01-01
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多