Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subclassing method with ellipsis array argument?

I would like to subclass an object that has the ellipsis syntax in the init header. i.e.

-(void) initObjectWith:(NSString*)argument arguments:(NSString*)someArgument,...;

I'm unsure how to pass along the arguments array in this case. I suspect it would be something like:

- (void) initObjectWithCustomInitializer:(NSString*)argument additionalArgument:(NSString*)additionalArgument argument:(NSString*) someArgument,... {
  self = [super initObjectWith:argument arguments:someArgument,...];
  if (self) {
     //custom init code here
  }
  return self
}

This compiles but the nil-terminated 'arguments' array is only getting the first argument. How do I pass along the objects of a nil-terminated array?

like image 356
Keller Avatar asked Dec 05 '22 12:12

Keller


1 Answers

The superclass that declares that variadic initializer should also declare a non-variadic one that takes a va_list (analogous to how printf has vprintf, for example). Assuming that case, where the superclass has both:

-(void)init:(id)a arguments:(id)b, ...;

and

-(void)init:(id)a arguments:(id)b variadicArgs:(va_list)args;

You would do something like:

- (void)myInit:(id)a newArg:(id)c arguments:(id)b, ...
{
    va_list v;
    va_start(v, b);

    self = [super init:a arguments:b variadicArgs:v];
    if (self) {
        //custom init code here
    }

    va_end(v);
    return self;
}

Of course, you should be sure to have a non-variadic version of your new initializer, too!

like image 199
Carl Norum Avatar answered Dec 28 '22 22:12

Carl Norum