Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XCTAssert syntax error with array shorthand expression and method call

So I am diving into unit tests with XCTest, and have run into a little problem.

When I write an XCTAssert statement with a shorthand array declaration and method call inside, a syntax error is spat out in Xcode:

XCTAssert([self hasStrings:@[ @"foo", @"bar" ]]);

In the Xcode compile errors:

Expected identifier or '('

However, if I add more brackets around the expression, it will work:

XCTAssert(([self hasStrings:@[ @"foo", @"bar" ]]));

Is this something to do with some of my syntax not being allowed to be passed into a macro? (Assuming that XCTAssert is a macro)

like image 426
elimirks Avatar asked Dec 19 '13 20:12

elimirks


1 Answers

XCTAssert is a macro with a variable argument list:

#define XCTAssert(expression, format...) \
    _XCTPrimitiveAssertTrue(expression, ## format)

In your first form, the preprocessor interprets

[self hasStrings:@[ @"foo", @"bar" ]]

as two macro arguments, separated by a comma.

like image 67
Martin R Avatar answered Nov 15 '22 08:11

Martin R