2

I'm trying to make a function similar to this:

#define printf_copy(s, ...)  printf(s, ##__VA_ARGS__)  // acceptable!

but that's a preprocessor, I need one for runtime, like this:

+ (NSString *)format:(NSString *)first, ...
{
    return [NSString stringWithFormat:first, __VA_ARGS__]; // unacceptable!
}

BUT!! this is unacceptable by compiler!

I'm trying to figure out whats the local variable for (...)? (yes those 3 dots)

  • I can't find anything in your question that is not answered by @Carl below. Care to either expound or mark his answer as accepted? – Mathew Apr 26 '13 at 01:24

1 Answers1

9

It's exactly the same as with C variadic functions. That means you can't just pass it through directly, you have to pass a va_list around. You'll need something like:

+ (NSString *)format:(NSString *)first, ...
{
    NSString *string;
    va_list args;

    va_start(args, first);
    string = [[NSString alloc] initWithFormat:first arguments:args];
    va_end(args);

    return [string autorelease];
}
Carl Norum
  • 219,201
  • 40
  • 422
  • 469