preface

I want to implement a macro like NSLog that supports multiple arguments, formatting and other logical processing

implementation

  1. String formatting
+(NSString*)getFormatStr:(NSString*)str, ... { if ([str isKindOfClass:[NSString class]]) { va_list args; va_start(args, str); NSString *result = [[NSString alloc] initWithFormat:str arguments:args]; va_end (args); return result; } else { return [NSString stringWithFormat:@"%@",str]; }}Copy the code
  1. Macro inside the realization of multiple parameter input
#define kGetFormatStr(Msg, ... ) [self getFormatStr:(Msg), ##__VA_ARGS__]
Copy the code

Note: # #VA_ARGSYou have to lose, otherwise the data from the previous macro call will contaminate the data from the next method. Examples of pollution data:

  1. Final implementation: