Abdou023 Abdou023 - 6 months ago 15
Swift Question

Remove elemnt of custom class from an array

I have an array of objects of custom class

Component
which doesn't subclass anything, and I want to remove an element from it:

var components: [Component] = [comp1, comp2, comp3]

components = components.filter({$0 != comp2}) // Error


the error:

Binary operator '!=' cannot be applied to two 'Component' operands

Answer

Use !== instead which returns true if two objects do not refer to the same instance.

Comments