• 【Swift 60秒】03 - Multi-line strings


    0x00 Lesson

    Standard Swift strings use double quotes, but you can’t include line breaks in there.

    If you want multi-line strings you need slightly different syntax: start and end with three double quote marks, like this:

    var str1 =  """
    This goes 
    over multiple
    lines
    """
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Swift is very particular about how you write those quote marks: the opening and closing
    triple must be on their own line, but opening and closing line breaks won’t be included in your
    final string.

    If you only want multi-line strings to format your code neatly, and you don’t want those line breaks to actually be in your string, end each line with a \, like this:

    var str1 =  """
    This goes \
    over multiple \
    lines
    """
    
    • 1
    • 2
    • 3
    • 4
    • 5

    0x01 Tips

    You can write \n in any string to add a line break inside your text.


    0x02 Test

    This code creates multi-line strings correctly - true or false?

    01

    var henley = """I am the master
    of my fate
    I am the captain of my soul"""
    
    • 1
    • 2
    • 3

    02

    var eliot = "This is the way the world ends
    Not with a bang but with a whimper"
    
    • 1
    • 2

    03

    var joseph = """
    When I am an old woman,
    I shall wear purple
    with a red hat that doesn't go,
    and doesn't suit me
    """
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    04

    var coleridge = """
    Water, water, everywhere
    and not a drop to drink"""
    
    • 1
    • 2
    • 3

    Answer
    01 - false
    The final three quotes must be on a line by themselves.

    02 - false
    Multi-line strings must start and end with three double quotes on lines by themselves.

    03 - true

    04 - false
    The final three quotes must be on a line by themselves.


    0x03 我的小作品

    欢迎体验我的作品之一:小笔记-XNote
    笔记一步到位!
    App Store 搜索即可~


  • 相关阅读:
    二分模板代码
    红外线相关的论文(可见光和红外图像融合、红外图像增强、红外图像目标检测、红外图像分割...)
    LCR 147.最小栈
    SRC实战-cookie注入漏洞
    kubesphare 学习尚硅谷
    国家高新技术企业的好处
    C++学习:this指针
    python 文件分割成几份
    金三银四来了-找工作有哪些平台/工具?
    el-select 绑定对象和el-checkbox-group用法
  • 原文地址:https://blog.csdn.net/xjh093/article/details/126638321