首页 > 解决方案 > 我想在字符串类型变量中添加多个变量,并且每个值都将从 swift 4 的下一行开始

问题描述

我有一个变量 var 结果:String = ""

现在我在不同的变量中得到不同的字符串,我想将这些值分配给我的结果变量,每个值都将在新行中。即这是我的代码

结果 = 结果 + (方法 ?? "") 结果 = 结果 + "\n"

    result = result + (content ?? "" )
    result = result + "\n"

    result = result + (acceptHeader ?? "")
    result = result + "\n"

    result = result + (url ?? "")
    result = result + "\n"

    result = result + (timeStamp ?? "")
    result = result + "\n"

    result = result + (publicKey ?? "")

我正在打印这个结果 它正在打印我这个

get
application/json
https://www.google.com
2020-04-07T11:08:34.989Z

但是当我将此值分配给某个变量时,它会给出这个 get\n\napplication/json\nhttps://www.google.com\n2020-04-07T11:08:34.989Z\n

我希望我的值将被分配给变量,就像它被打印出来一样。即像这样

get
application/json
https://www.google.com
2020-04-07T11:08:34.989Z

标签: swiftstringconcatenationswift4.2

解决方案


推荐阅读