Swift - Split string over multiple lines

前端 未结 15 2018
清酒与你
清酒与你 2020-12-04 07:51

How could I split a string over multiple lines such as below?

var text:String = \"This is some text
                   over multiple lines\"
15条回答
  •  离开以前
    2020-12-04 08:39

    As pointed out by litso, repeated use of the +-Operator in one expression can lead to XCode Beta hanging (just checked with XCode 6 Beta 5): Xcode 6 Beta not compiling

    An alternative for multiline strings for now is to use an array of strings and reduce it with +:

    var text = ["This is some text ",
                "over multiple lines"].reduce("", +)
    

    Or, arguably simpler, using join:

    var text = "".join(["This is some text ",
                        "over multiple lines"])
    

提交回复
热议问题