Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does passing an NSString object to the "format" parameter of XCTAssertTrue cause a build error?

Tags:

In attempting to use XCTest to test my application, I get a build error when doing the following:

#import <XCTest/XCTest.h>

@interface MyTests : XCTestCase

@end

@implementation MyTests

- (void)testExample
{
    NSString *str = @"foo";
    XCTAssertTrue(YES, str); // Parse issue: Expected ')'
}

@end

but I do not get a build error if I do this:

#import <XCTest/XCTest.h>

@interface MyTests : XCTestCase

@end

@implementation MyTests

- (void)testExample
{
    XCTAssertTrue(YES, @"foo"); // this is just fine...
}

@end

The build error that I get is:

Parse issue: Expected ')' 

and it puts an arrow under the "s" in "str".

I discovered that I can remedy this by changing

XCTAssertTrue(YES, str)

to

XCTAssertTrue(YES, @"%@", str)

but I just cannot figure out why that makes a difference. Can somebody please explain why this is so?

like image 447
akivajgordon Avatar asked Nov 07 '13 05:11

akivajgordon


1 Answers

The XCT... macros are written to accept format strings — the strings themselves are optional (so that writing XCTAssertTrue(YES) is valid), but they must be constant strings. You cannot pass objects into the macro without having a format string, which is why XCTAssertTrue(YES, @"%@", str) works, but, say, XCTAssertTrue(YES, str) or XCTAssertTrue(NO, nil) wouldn't.

like image 171
Itai Ferber Avatar answered Oct 05 '22 23:10

Itai Ferber