我知道这是个不必要的问题但是..。为什么我不能使用减缩将字符数组转换为字符串?
例如,
let str = "this is a string"
let clist = Array(str)
let slist = clist.reduce("", +)给我:“字符”不是“Uint8”的子类型
list dlist = [0, 1, 2, 3, 4, 5, 6]
let sum = dlist.reduce(0, +)作品
我知道我可以简单地做slist = String(clist),但我只想知道,你知道吗?
在xcode 6.2操场上的Swift 1.1
谢谢!
发布于 2015-04-15 20:37:16
的combine:闭包中
let slist = clist.reduce("", +)$0是迄今为止积累的结果-a String,$1是来自clist -a Character的当前元素。没有以+作为参数的(String, Character)运算符。
这样做是可行的:
let slist = clist.reduce("") { $0 + String($1) }发布于 2015-04-15 20:38:36
在Swift 1.2中:
let str = "this is a string"
let clist = Array(str)
let slist = clist.map { String($0) }.reduce("", combine: { $0 + $1 })
println(slist) // "this is a string"https://stackoverflow.com/questions/29660242
复制相似问题