Clad Clad Clad Clad - 1 year ago 183
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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download