Clad Clad Clad Clad - 7 months ago 81
iOS Question

Objective C - #define using __VA_ARGS__

I am learning how to use macro but now confused with one.

I am trying to create a

concatenate which will just append every params to each other.

for example :
would return a
containing :

I actually made a bit of code (some found here as well) which get the number of params but I don't succeed to make the second part of the job.

#define NUMARGS(...) ( sizeof((int[]){__VA_ARGS__}) / sizeof(int) )
#define concatOP(...) NSMutableString *format = [[NSMutableString alloc] init];\
for( int i = 0; i < NUMARGS(__VA_ARGS__); i++){\
[format appendString:@"%@"];}\
[[NSString alloc] initWithFormat:format, __VA_ARGS__]

I actually get many errors, telling me that format doesn't exist or that I miss some ";" or other ending tags.


Here is yours macro:

#define concatOP(...) [@[__VA_ARGS__] componentsJoinedByString:@""]

EDIT: if you unwind yours macro NSString* result = concatOP(@"hey",@"Jude",@"Don't"); you will get:

NSString* result = NSMutableString *format = [[NSMutableString alloc] init]; for( int i = 0; i < NUMARGS(@"hey",@"Jude",@"Don't"); i++){ format = [format appendString:@"%@"];} [[NSString alloc] initWithFormat:format, @"hey",@"Jude",@"Don't"];

Looks odd.