質問

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)

役に立ちましたか?

解決

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];
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top