0

I'm trying to compare two object arrays like this:

if oneObjectArray != anotherObjectArray {
   // ... do stuff
}

However I get the following less than helpful compiler error:

Binary operator '!=' cannot be applied to operands of type '[MyModelObject]' and '[(MyModelObject)]'`

The compiler error points to the first operand in the equality check.

5
  • Type of the instances must be same when you compare it with if condition. Commented Aug 3, 2015 at 8:59
  • How are you declaring and instantiating the anotherObjectArray? Commented Aug 3, 2015 at 9:00
  • Also, are you checking for value equality or pointer equality? Are you checking that they are actually the same instance of an array or are you checking that they contain the same information? Commented Aug 3, 2015 at 9:01
  • I want to check array content equality, not array reference equality. However I now realise that == is not what I want. However, I still want to understand this specific compiler error, so I'm prepared for next time I see it. Both arrays are declared as [MyModelObject], however one is declared constant via let and the other is var. Commented Aug 3, 2015 at 9:08
  • OK. FYI == and != is exactly what you want :D Have you made MyObectModel Equatable? If not then this is probably what your problem is. (If you're not sure then you haven't). Commented Aug 3, 2015 at 9:27

1 Answer 1

1

I think that your problem is that you have not made MyObectModel equatable.

In order to check equality of two arrays of MyObjectModel you need to be able to check equality of two MyObjectModel objects.

To do this you need to do the following...

extension MyObjectModel: Equatable {}

// as a top level function
func ==(lhs: MyObjectModel, rhs: MyObjectModel) -> Bool {
    // check if your objects are equal here...
    return lhs.name == rhs.name
}
Sign up to request clarification or add additional context in comments.

1 Comment

Spot-on thanks! I thought I had already done this, but had mentally mixed up which model class I was dealing with. Really shouldn't start coding until fully caffeinated on a Monday morning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.