Variadic function in obj-C skips first argument

I have a problem passing a variable to a function with parameters:

-(void)addCharacterToScene:(NSString *)name withFrames:(CCSpriteFrame*)frames,... { va_list args; va_start(args, frames); id arg = va_arg(args,CCSpriteFrame*); int i=1; while (arg) { NSString *frame_name = [NSString stringWithFormat:@"%@_%i",name,i]; NSLog(@"%@ \n%@",frame_name, arg); [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFrame:arg name:frame_name]; arg = va_arg(args,CCSpriteFrame*); i++; } va_end(args); } 

The problem is that the function skips the first argument. My function call is as follows:

 [self addCharacterToScene:@"wiz" withFrames:wizardFrame1,wizardFrame2,wizardFrame3,nil]; 

I can pass the dummy object to the first position and give the desired result, but there should be a better solution. Thank you

+4
source share
1 answer

Instead, for the first time:

 id arg = va_arg(args,CCSpriteFrame*); 

do:

 id arg = frames; 

A va_list starts after the argument specified in the va_start function. Therefore, if you want to process this argument, you must start with it yourself.

+6
source

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


All Articles