Search code examples
arraysswiftbinary-operators

Binary operator '!=' cannot be applied to two '[[String]]' operands


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...


Solution

  • 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: ==) }
    

    (Note: Thanks to SE-0143 "Conditional conformances", this workaround is no longer needed once Swift 4 is released.)