将数组元素与"\n"合并到Swift中的String中

May*_*mur 3 arrays string ios swift

我正在尝试合并字符串数组\n,但没有得到任何解决方案.我有以下类型的数组,其中包含节名称.

let arrSectionName = [
    "Section 1",
    "Section 2",
    "Section 3",
    "Section 4",
    "Section 5",
    "Section 6",
]
Run Code Online (Sandbox Code Playgroud)

现在,我想从上面的数组中获取此字符串:

"第1节\n第2节\n第3节\n第4节\n第5节\n第6节"

所以最后消息应显示如下:

警报消息输出:

    Following section which are still pending to complete, please verify and try again:
    Section 1
    Section 2
    Section 3
    Section 4
    Section 5
    Section 6
Run Code Online (Sandbox Code Playgroud)

节名称是动态的,将更改运行时和存储arrSectionName.

Raj*_*r R 7

你应该使用加入

let str = arrSectionName.joined(separator: " \n")
Run Code Online (Sandbox Code Playgroud)