Xcode: XCTest cannot no compare two NSArray of numbers

882 Views Asked by At

I'm trying to implement a unit test(XCTest) to compare two NSArray objects:

Here is my implementation:

- (void)testTwoNumsArrays {
    NSArray *result = @[@20,@20];
    NSArray *expecteResult = @[@20,@20];
    
    XCTAssertEqual(result, expecteResult);
}

I'm getting this error:

((result) equal to (expecteResult)) failed: ("{length = 8, bytes = 0xb0cfc00001000000}") is not equal to ("{length = 8, bytes = 0x9032ce0001000000}")

Any of you know why I can not compare the two arrays. In Swift it works just fine.

I'll really appreciate your help.

1

There are 1 best solutions below

0
On

The problem is you are comparing the pointers to the objects and not the objects themselves.

XCTAssertEqual is essentially doing a check like the following, which is only comparing the pointer values.

if (a != b) assert();

You want something more along the lines of

if (![a isEqual:b]) assert();

This can be achieved with the XCTAssertEqualObjects call.

More information:

XCTAssertEqual fails to compare two string values?

https://developer.apple.com/documentation/xctest/xctassertequalobjects