[英]Why does passing an NSString object to the “format” parameter of XCTAssertTrue cause a build error?
In attempting to use XCTest to test my application, I get a build error when doing the following: 在尝试使用XCTest测试我的应用程序时,执行以下操作时出现构建错误:
#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". 它在“str”中的“s”下面放了一个箭头。
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? 有人可以解释为什么会这样吗?
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. 编写XCT...
宏以接受格式字符串 - 字符串本身是可选的(因此编写XCTAssertTrue(YES)
是有效的),但它们必须是常量字符串。 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. 如果没有格式字符串,则无法将对象传递到宏中,这就是XCTAssertTrue(YES, @"%@", str)
工作的原因,但是,例如, XCTAssertTrue(YES, str)
或XCTAssertTrue(NO, nil)
不会。
Deep inside the implementation, the code does this: 在实现的深处,代码执行此操作:
@"" format
If format
is a constant string literal, the compiler concatenates the strings. 如果format
是常量字符串文字,则编译器将字符串连接起来。 If format
is anything else, you get a compiler error. 如果format
是其他任何东西,则会出现编译器错误。
Passing predefined text into the assertion is sometimes desirable so: 将预定义文本传递给断言有时是可取的:
XCTAssertTrue(YES, @"foo"); // this is just fine...
As is this 就是这样
#define FOO @"foo"
XCTAssertTrue(YES, FOO); // this is just fine too...
So I do stuff like: 所以我做的事情如下:
#define DBUEqualityTestFailed @"Equality test failed"
// test
DBNumber *n1 = [@((int)1) dbNumberFromIntValue];
XCTAssertTrue(*(int *)[n1 valuePointer] == 1, DBUEqualityTestFailed);
XCTAssertTrue([n1 valuePointer] == [n1 valuePointer], DBUEqualityTestFailed);
XCTAssertTrue(*(int *)[n1 valuePointer] == *(int *)[n1 valuePointer], DBUEqualityTestFailed);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.