例如:
var a = [1, 2, 3] // Ints
var s = ",".join(a) // EXC_BAD_ACCESS
Run Code Online (Sandbox Code Playgroud)
是否可以使连接函数返回"1,2,3"?
扩展Int(或其他自定义类型)以符合某些协议?
我知道如果我想将一个Ints 数组转换为a String,我这样做:
[0,1,1,0].map{"\($0)"}.reduce(""){$0+$1}
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何将一个Ints 数组转换为逗号分隔String
我有一个变量的字符串数组,另一个变量的字符串.我想将集合中的所有字符串追加到单个字符串中.
所以我举例说:
var s = String()
//have the CSV writer create all the columns needed as an array of strings
let arrayOfStrings: [String] = csvReport.map{GenerateRow($0)}
// now that we have all the strings, append each one
arrayOfStrings.map(s.stringByAppendingString({$0}))
Run Code Online (Sandbox Code Playgroud)
上面的一行失败了.我已经尝试了我能想到的每一个组合,但是在一天结束时,除非我只是创建一个for循环遍历整个集合,arrayOfStrings并逐个添加它,否则我无法得到它.我觉得我可以使用地图或其他功能以同样的方式实现这一点.
有帮助吗?
谢谢!
我正在尝试合并字符串数组\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.
如何将数组[Int]转换为例如保持元素之间逗号的字符串?如果我有一个like数组[1,2,3,4],我想收到一个String like "1, 2, 3, 4"。
swift ×5
ios ×3
arrays ×2
closures ×1
cocoa-touch ×1
foundation ×1
ios9 ×1
string ×1
swift2 ×1