Wednesday, September 18, 2019

Using a Macro Based Function Within an XCTest Assert Macro

Using NSString initWithFormat: option within an XCTAssertEqualObjects directly will given an error.
XCTAssertEqualObjects([[NSString alloc] initWithFormat:@"Hello %@", @"olive"], @"Hello olive");
This will show error in Xcode like:
@try statement without a @catch and @finally clause
Expected ']'
Expected identifier or '('
Expected identifier or '('
Extraneous closing brace ('}')
Unexpected '@' in program
This is because the XCTAssertEqualObjects is a macro and it accepts va_args and initWithFormat: also accepts va_args. So there is a conflict and we need to wrap it within a parentheses.
XCTAssertEqualObjects(([[NSString alloc] initWithFormat:@"Hello %@", @"olive"]), @"Hello olive");