[英]Binary operator '+' cannot be applied to operands of type '_' and 'String'
I am new to swift.我是 swift 的新手。 I want to use the function reduce to concatenate names in an array my code:
我想使用函数 reduce 将我的代码数组中的名称连接起来:
let names = ["ZZZZZZ", "B", "AA", "CCCC", "EEEEE"]
let sum = names.reduce(0) {
return $0 + $1
}
and I get this error Binary operator '+' cannot be applied to operands of type '_' and 'String'并且我收到此错误二进制运算符“+”不能应用于“_”和“字符串”类型的操作数
If you want to reduce
strings the default value must be an empty string ""
如果要
reduce
字符串,默认值必须是空字符串""
let sum = names.reduce("") { return $0 + $1 }
Actually实际上
let sum = names.joined()
does the same.做同样的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.