Second argument to va_arg is of non-POD type

Published on the 12th of July 2011

A really specific error I — nonetheless — wanted to share..

After installing the iOS5 beta 3 last night I got the following build error while compiling Tanis: error: Second argument to 'va_arg' is of non-POD type 'id'.

My code looks something like this:

@interface NSMutableArray (variadicMethodExample)

- (void) appendObjects:(id) firstObject, ...; 
// This method takes a nil-terminated list of objects.

@end

@implementation NSMutableArray (variadicMethodExample)

- (void) appendObjects:(id) firstObject, ...
{
        id eachObject;
        va_list argumentList;
        if (firstObject) // The first argument isn't part of the varargs list,
        {                                   // so we'll handle it separately.
                [self addObject: firstObject];
                va_start(argumentList, firstObject); // Start scanning for arguments after firstObject.

                while (eachObject = va_arg(argumentList, id)) // As many times as we can get an argument of type "id"

                [self addObject: eachObject]; // that isn't nil, add it to self's contents.

                va_end(argumentList);

        }
}
@end

Apple told me it's a bug in the compiler. They will likely address it in a future seed. In the mean time I've used this workaround to temporarily fix my error:

void *x = va_arg(argumentList, void*);
id y = (__bridge id)x;

The idea is to 'bridge' the argument back to a id type. Nasty, I know, but it's suggested by one of Apple's own developers.