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?
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With