Variable Arguments in ObjC iPhone Function

I have to get stuck on stoopid today because I spent more than an hour trying to figure out how to get the args variables to work in this iPhone project I'm working on. Can someone help me get the green bar below unit test? Where am I mistaken?

#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 
+4
source share
2 answers

Change this:

 NSString *result = vaArgsAppend(@"one ", "two ", @"three"); 

:

 NSString *result = vaArgsAppend(@"one ", @"two ", @"three", nil); 

When you write a variational method, you should have a means to determine the number of arguments to read. The most common way to do this is to find the end value in the list you go to. You do not click on your terminal state.

+6
source

In addition, "two" is const char *, not id. All sorts of funny consequences can arise if you consider it as id. Replace with two.

Also, if (eachArg) is extraneous.

+3
source

Source: https://habr.com/ru/post/1301375/


All Articles