I must be stuck on stoopid today because I spent over an hour trying to understand how to make variable args work in this iPhone project I’m working on. Could somebody help me get a green bar in the below unit test? Where am I going wrong?
#import <SenTestingKit/SenTestingKit.h>
@interface VAArgsTest : SenTestCase
{
}
@end
NSString* vaArgsAppend(NSString *first, ...)
{
NSMutableString *list = [[NSMutableString alloc] initWithString:first];
id eachArg;
va_list argumentList;
va_start(argumentList, first);
while(eachArg = va_arg(argumentList, id)) {
if(eachArg)[list appendString:(NSString*)eachArg];
}
va_end(argumentList);
return [list autorelease];
}
@implementation VAArgsTest
-(void) testCallVaArgsAppend
{
NSString *result = vaArgsAppend(@"one ", "two ", @"three");
STAssertEqualObjects(result, @"one two three", @"Expected appended string.");
}
@end
Change this:
to this:
When you write a variadic method, you have to have a means to determine how many arguments to read. The most common way to do this is to look for a terminating value in the list you pass in. You’re not hitting your terminal condition.