daveoncode daveoncode - 1 year ago 111
Objective-C Question

"va_start used in function with fixed args" exception?

I'm trying to create a simple macro using varargs, but I'm getting "va_start used in function with fixed args" and I don't understand why and where I'm wrong. The code is the following:

#define FOO(obj, ...) \
va_list args; \
va_start(args, obj); \
NSString *currentObject; \
while ((currentObject = va_arg(args, NSString*)) != nil) { \
NSLog(@"string: %@", currentObject); \
} \

Answer Source

The va_list type and its associated operations are for variadic functions. They don't work for variadic macros, which use __VA_ARGS__ and work somewhat differently.

In this example I can't see any reason to use a macro; you should probably just use a function instead. If it turns out that you do need a macro, you will probably end up having to pass __VA_ARGS__ as the arguments to a variadic function, which can then unpack the arguments and perform further work.