@#在C预处理器中意味着什么?

时间:2016-01-30 00:07:15

标签: c-preprocessor

@#expression1, expression2, @#expression2在下面的上下文中意味着什么?

代码是Objective-C,但Objective-C仍然使用C预处理器。

#define XCTAssertEqualObjects(expression1, expression2, ...) \
    _XCTPrimitiveAssertEqualObjects(self, expression1, @#expression1, expression2, @#expression2, __VA_ARGS__)

#define _XCTPrimitiveAssertEqualObjects(test, expression1, expressionStr1, expression2, expressionStr2, ...) \
({ \
    @try { \
        id expressionValue1 = (expression1); \
        id expressionValue2 = (expression2); \
        if ((expressionValue1 != expressionValue2) && ![expressionValue1 isEqual:expressionValue2]) { \
            _XCTRegisterFailure(test, _XCTFailureDescription(_XCTAssertion_EqualObjects, 0, expressionStr1, expressionStr2, expressionValue1, expressionValue2), __VA_ARGS__); \
        } \
    } \
    @catch (_XCTestCaseInterruptionException *interruption) { [interruption raise]; } \
    @catch (NSException *exception) { \
        _XCTRegisterFailure(test, _XCTFailureDescription(_XCTAssertion_EqualObjects, 1, expressionStr1, expressionStr2, [exception reason]), __VA_ARGS__); \
    } \
    @catch (...) { \
        _XCTRegisterFailure(test, _XCTFailureDescription(_XCTAssertion_EqualObjects, 2, expressionStr1, expressionStr2), __VA_ARGS__); \
    } \
})

1 个答案:

答案 0 :(得分:2)

XCTAssertEqualObjects正在将您正在验证的表达式转换为字符串,因此如果失败,它可以打印失败的表达式(通过_XCTPrimitiveAssertEqualObjects_XCTRegisterFailure)。

在C预处理器中,使用#stringify传入的任何内容,因此您可以将其用作常量。通过将@加到字符串前面,它就变成了一个Objective-C字符串常量。