简体   繁体   English

二元运算符“+”不能应用于“_”和“String”类型的操作数

[英]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.

相关问题 二元运算符“+”不能应用于“字符串”和“字符串?”类型的操作数 - Binary operator '+' cannot be applied to operands of type 'String' and 'String?' 二元运算符“==”不能应用于“[String]”和“String”类型的操作数 - Binary operator '==' cannot be applied to operands of type '[String]' and 'String 二进制运算符'+'不能应用于'String'和'()-> String'类型的操作数 - Binary operator '+' cannot be applied to operands of type 'String' and '() -> String' 二进制运算符'〜='不能应用于'String'和'String?'类型的操作数 - Binary operator '~=' cannot be applied to operands of type 'String' and 'String?' 二进制运算符“ +”不能应用于类型为“字符串”和“字符串感叹号”的操作数 - Binary operator '+' cannot be applied to operands of type 'String' and 'String exclamationmark 二进制运算符'=='不能应用于'NSObject'和'String'类型的操作数 - Binary operator '==' cannot be applied to operands of type 'NSObject' and 'String' 二进制运算符-不能应用于字符串类型的操作数! 和诠释 - Binary operator - cannot be applied to operands of type String! and Int 二进制运算符'=='不能应用于'Any?'类型的操作数 和'String'Swift iOS - Binary operator '==' cannot be applied to operands of type 'Any?' and 'String' Swift iOS 二进制运算符'=='不能应用于类型'UIImage?'的操作数 和“字符串”-Swift 4 - Binary operator '==' cannot be applied to operands of type 'UIImage?' and 'String' - Swift 4 Swift-二进制运算符'> ='不能应用于类型为'String'和'Int'的操作数 - Swift - Binary operator '>=' cannot be applied to operands of type 'String' and 'Int'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM