二进制运算符'!='不能应用于两个'[[String]]'操作数 [英] Binary operator '!=' cannot be applied to two '[[String]]' operands
问题描述
我有一个数组数组,需要过滤掉其中的一个特定数组.但是,使用以下代码时,出现二进制运算符'!='不能应用于两个'[[String]]'操作数"的问题.
I have an array of arrays and need to filter out one of the specific arrays in it. However, when using the following code, I get the issue "Binary operator '!=' cannot be applied to two '[[String]]' operands".
var arrayOfArrays = [[[String]]]()
var specificArray = [[String]]()
arrayOfArrays = arrayOfArrays.filter{$0 != specificArray}
我认为这曾经在半年前起作用...
I think this used to work like half a year ago...
推荐答案
如评论中所述, Swift数组不符合Equatable ,因此 [[T]]!= [[T]]
不起作用,因为它要求 [T]
为Equatable.您可以使用 elementsEqual(_:by:)
,它允许使用自定义的相等函数比较元素,而不会变得相等:
As mentioned in the comments, Swift Arrays don't conform to Equatable so [[T]] != [[T]]
does not work because it requires [T]
to be Equatable. You could use elementsEqual(_:by:)
instead, which allows comparing elements using a custom equality function, without being Equatable:
arrayOfArrays = arrayOfArrays.filter { !$0.elementsEqual(specificArray, by: ==) }
(注意:感谢 SE-0143"条件一致" ,一旦Swift 4发布,就不再需要此解决方法.)
(Note: Thanks to SE-0143 "Conditional conformances", this workaround is no longer needed once Swift 4 is released.)
这篇关于二进制运算符'!='不能应用于两个'[[String]]'操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!