Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When would initWithFormat:arguments: be used?

Tags:

cocoa

nsstring

The NSString method initWithFormat:arguments: takes a va_list as an argument. I can't figure out when it would be used (or even how to use it). Why would Apple add such a method when the regular initWithFormat: is much more user-friendly?

like image 557
calvinlough Avatar asked Feb 11 '10 02:02

calvinlough


1 Answers

You can't pass a dynamic list of format arguments to -initWithFormat:. For example, if you wanted to implement -stringByAppendingFormat: yourself without -initWithFormat:arguments:, you'd have a job of it. With the va_list version, you could do it:

- (NSString *)stringByAppendingFormat:(NSString *)format, ... {
    va_list args;
    va_start(args, format);
    NSString * result = [self stringByAppendingString:[NSString stringWithFormat:format arguments:args]];
    va_end(args);
    return result;
}
like image 51
John Calsbeek Avatar answered Oct 02 '22 02:10

John Calsbeek