Handing off varargs? (Language lawyers, please read.)
Handing off varargs? (Language lawyers, please read.)
- Subject: Handing off varargs? (Language lawyers, please read.)
- From: "John C. Randolph" <email@hidden>
- Date: Thu, 16 Aug 2001 06:36:09 -0700
I've got a bit of a weird situation, where I'd like an instance method
that takes a variable argument list to hand off that list to a class
method which also takes a variable argument list.
So, is this kosher? (it works, but I have this nagging suspicion that
it's going to bite me someday...)
@implementation NSString (concatExtensions)
- (NSString *) stringByAppendingStrings:(NSString *) aString, ...
/*"This instance method returns an autoreleased string which is the
result of concatenating the receiver and all of the arguments given.
The list needs to be null-terminated."*/
{
return [self stringByAppendingString:[[self class]
stringByConcatenatingStrings:aString]];
}
/*This is the part that troubles me. I don't think I should be able to
get away with just passing aString, and I don't know how the method
below is getting the whole list, but it does; I've tried it!*/
+ (NSString *) stringByConcatenatingStrings:(NSString *) aString, ...
/*"This class method returns an autoreleased string which is the result
of concatenating all of the arguments given. The list needs to be
null-terminated."*/
{
va_list
strings;
NSString
*thisString,
*result = aString;
va_start(strings, aString);
while (thisString = va_arg(strings, id))
result = [result stringByAppendingString:thisString];
va_end(strings);
return result;
}
@end
Support your right to protest a criminal nut-cult.
http://freehenson.da.ru/