0

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

Julian Lee
  • 293
  • 1
  • 11

1 Answers1

1

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

Community
  • 1
  • 1
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • This is exactly what I was looking for. I see the part where I was confused regarding the other post, thank you for the explanation! – Julian Lee Mar 22 '17 at 10:36