arrays - Binary operator '+' cannot be applied to two 'Set' operands Error
问题描述
I have 2 Arrays and I want to merge them after converting them to Sets, this is my code:
var before = Array(Set(beforevalues))
var first = Array(Set(myvalues))
let save1 = first{ ($0.rangeOfString("today") != nil) }
let save2 = Set(save1)
var mynewarray = Set(save2)
let merge = Array(Set(before + mynewarray)) // THIS LINE ERROR
but it gives:
Binary operator '+' cannot be applied to two 'Set' operands
How can I fix it?
解决方案
You need to use union
to merge two Set
s as there's no +
operator defined for Set
s. Moreover, before
is an Array
while mynewarray
is a Set
, but since Set.union
accepts an input argument of type Sequence
you can solve that issue by calling union
on the Set
.
let merged = Array(mynewarray.union(before))
推荐阅读
- sas - SAS中只输出ROC曲线
- css - 如何设置 XML 驱动的 URL 的样式
- ruby - 如何向 Edge/IE WebDriver 传递/创建虚假媒体流?
- android - 如何检查recyclerView中是否存在新添加的项目?
- hyperledger-fabric - 无法将 Hyperledger Fabric Marbles 链代码从存储库安装到对等点
- node.js - Graphql 与非 Graphql React-Mongoose App 的区别
- jenkins - 无论如何使用Jenkins将git repo从一个EC2实例复制到另一个EC2实例?
- datetime - UTC 到本地时区之间的 C# DateTime 转换
- cuda - 如何使用占位符结合推力比较?
- python-3.x - Python3 - 打印字符串和列表