Re: Handing off varargs? (Language lawyers, please read.)
Re: Handing off varargs? (Language lawyers, please read.)
- Subject: Re: Handing off varargs? (Language lawyers, please read.)
- From: Chris Kane <email@hidden>
- Date: Thu, 16 Aug 2001 11:02:36 -0700
No, not kosher. Dangerous, if you care about the code not mysteriously
breaking when you compile it with a new version of a compiler someday.
You just need to create a va_list version of your second method, which
you do by deleting the internal va_ stuff and a new parameter:
+ (NSString *)stringByConcatenatingString:(NSString *)aString
withArgs:(va_list)strings
{
NSString
*thisString,
*result = aString;
while (thisString = va_arg(strings, id))
result = [result stringByAppendingString:thisString];
return result;
}
You can make the other method:
+ (NSString *) stringByConcatenatingStrings:(NSString *) aString, ...
{
va_list
strings;
NSString
*result;
va_start(strings, aString);
result = [self stringByConcatenatingString:aString withArgs:strings];
va_end(strings);
return result;
}
call into the new one for its implementation, if you want. "Reuse"
doesn't buy you very much in this case. Or there are other variations
on the same idea.
Chris Kane
Cocoa Frameworks, Apple
On Thursday, August 16, 2001, at 06:36 AM, John C. Randolph wrote:
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/
_______________________________________________
cocoa-dev mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/cocoa-dev