Unfortunately, although there's no particular reason for it, you're not allowed to write a method that takes only variadic arguments. This doesn't work:
- (NSString *)format: ...;
It would be oh-so convenient, meaning you could write formatting operations like this:
- (NSString *)format:(id)first, ... {
NSUInteger argCount = 0;
BOOL prevPercent = NO;
for (NSUInteger i = 0; i < [self length]; ++i) {
unichar c = [self characterAtIndex:i];
if (prevPercent && c != '%')
++argCount;
prevPercent = c == '%';
}
NSMutableArray *argArr = [NSMutableArray new];
va_list args;
va_start(args, first);
for (NSUInteger i = 0; i < argCount; ++i)
[argArr addObject:va_arg(args, id)];
va_end(args);
switch (argCount) {
case 0: return [NSString stringWithFormat:self, first];
case 1: return [NSString stringWithFormat:self, first, argArr[0]];
case 2: return [NSString stringWithFormat:self, first, argArr[0], argArr[1]];
// ...
}
}
Only supports NSObject arguments, obviously. If you actually wanted to do this approach you'd want to do a better job of parsing the format string and use a more cleverer way of storing the args.
Or, for the completely insane approach that probably doesn't actually work (on top of being entirely unportable):
The problem people have with the format implementation is that no matter what you do, it's either obtuse shorthand, or incredibly verbose.
The reality is, that NSString needs native formatting sugar. Anything else would really just be a hack. I'm not particularly even happy with the [@"Hello ":@"World] syntax, either.
I'm not super comfortable with breaking message syntax like that. Also, very wary of using a #define with a short oft used keyword in a library that will be imported in majority of headers.