预处理器将每个可变参数重新格式化为其每个字符串值+值

Preprocessor to reformat each variadic argument to their each string value+value

我想用名称扩展每个参数,然后是值,例如:

#define LOG_VARIABLES(FORMAT, ...) NSLog(FORMAT, ##__VA_ARGS__) //Not sure what to do with ##__VA_ARGS__ here to make it expands like as described below

LOG_VARIABLES(@"%@: %@, %@: %@, %@: %@", arg1, arg2, arg3)

会扩展到

STRING_VALUE__(arg1), arg1, STRING_VALUE__(arg2), arg2, STRING_VALUE__(arg3), arg3

所以 NSLog 打印:

arg1: <arg1_value>, arg2: <arg2_value>, arg3: <arg3_value>

Overloading Macro on Number of Arguments 的一点帮助下,您可以这样做。

#define LOG_VARIABLES( FORMAT, ...) NSLog( FORMAT, __VA_ARGS__ )

#define STR(X) #X
#define VAL(X) @(STR(X)), X

#define LOG1( FORMAT, A )                LOG_VARIABLES( FORMAT, VAL( A ) )
#define LOG2( FORMAT, A, B )             LOG_VARIABLES( FORMAT, VAL( A ), VAL( B ) )
#define LOG3( FORMAT, A, B, C )          LOG_VARIABLES( FORMAT, VAL( A ), VAL( B ), VAL( C ) )
#define LOG4( FORMAT, A, B, C, D )       LOG_VARIABLES( FORMAT, VAL( A ), VAL( B ), VAL( C ), VAL( D ) )
#define LOG5( FORMAT, A, B, C, D, E )    LOG_VARIABLES( FORMAT, VAL( A ), VAL( B ), VAL( C ), VAL( D ), VAL( E ) )
#define LOG6( FORMAT, A, B, C, D, E, F ) LOG_VARIABLES( FORMAT, VAL( A ), VAL( B ), VAL( C ), VAL( D ), VAL( E ), VAL( F ) )

#define GET_MACRO( FORMAT, _1, _2, _3, _4, _5, _6, NAME, ... ) NAME
#define LOG_X(FORMAT,...) GET_MACRO(_0, ##__VA_ARGS__, LOG6, LOG5, LOG4, LOG3, LOG2, LOG1 )(FORMAT,__VA_ARGS__)

int main ( int argc, const char * argv [] ) {
    @autoreleasepool
    {
        // insert code here...
        NSLog(@"Hello, World!");

        NSString * a = @"aa";
        NSString * b = @"bb";
        NSString * c = @"cc";
        NSString * d = @"dd";
        NSString * e = @"ee";
        NSString * f = @"ff";

        LOG_X(@"%@:%@",a);
        LOG_X(@"%@:%@ and %@:%@",a,b);
        LOG_X(@"%@:%@ and %@:%@ and %@:%@",a,b,c);
        LOG_X(@"%@:%@ and %@:%@ and %@:%@ and %@:%@",a,b,c,d);
        LOG_X(@"%@:%@ and %@:%@ and %@:%@ and %@:%@ and %@:%@",a,b,c,d,e);
        LOG_X(@"%@:%@ and %@:%@ and %@:%@ and %@:%@ and %@:%@ and %@:%@",a,b,c,d,e,f);

    }
    return 0;
}

让我赶紧补充一点,可能有更好的方法来完成你想要做的事情,这只是一个只适用于 6 个参数的示例,但你明白了......而且,我开始了与你的 LOG_VARIABLES 但一路上它变成了 LOG_X.