简体   繁体   English

为什么将NSString对象传递给XCTAssertTrue的“format”参数会导致构建错误?

[英]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.

相关问题 NSstring,IBAction中格式错误 - NSstring with format error in IBAction 为什么Swift 3对OSX和iOS版本的NSString会有不同的对待? - Why does Swift 3 treat NSString differently for OSX vs iOS build? 测试iOS UI时XCTAssertTrue不会失败 - XCTAssertTrue does not fail when testing iOS UI 为什么我的“包含 SwiftyDropbox”会导致我的 iOS Xcode Swift 项目出现构建错误? - Why does my "include SwiftyDropbox" cause a build error for my iOS Xcode Swift project? 为什么Cordova构建大于6.3.1导致错误:源路径不存在:resources \\ ios \\ icon \\ - Why Cordova build greater then 6.3.1 cause Error: Source path does not exist: resources\ios\icon\ 将NSNumber对象分配给NSString对象不会导致任何错误或警告 - assigning NSNumber object to NSString object won't cause any error or warning 为什么静态NSString泄漏? - Why does static NSString leak? replaceObjectAtIndex:withObject:传入NSString对象时不起作用 - replaceObjectAtIndex:withObject: not working when passing in a NSString object 将 healthkit 返回的 HKBiologicalSex 对象格式化为 NSString - Format HKBiologicalSex object returned by healthkit to NSString 为什么对所有四个方面都添加约束会导致错误? - Why does adding constraints for all four sides cause an error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM