Take the 2-minute tour ×

This question already has an answer here:

I understand the usage of key words "va_list" "va_start" "va_arg" "va_end". And my supper class has a init method looks like this:

Super Class:

- (id) initWithChildren:(NSObject*)firstChild, ... NS_REQUIRES_NIL_TERMINATION{
    if(self = [super init]){
        va_list children;
        va_start(children, firstChild);

        self.children = [[NSMutableArray alloc] initWithObjects:firstChild, nil];
        firstChild.father = self;

        NSObject* child;
        while ((child = va_arg(children, NSObject*)) != nil){
            [_children addObject:child];
    return self;

It works well.But I have trouble to inherit it.

Sub Class

- (id) initWithName:(NSString*)name children:(NSObject*)firstChild, ... NS_REQUIRES_NIL_TERMINATION{
    self = [super initWithChildren:"what should I write here?"];
    if (self){
        self.name = name;
        //other subclass work
    return self;

Any ideas?Thanks.

share|improve this question
add comment

marked as duplicate by rmaddy, CodaFi, ughoavgfhw, Andrew Madsen, bbum Feb 7 '13 at 4:01


1 Answer

up vote 2 down vote accepted

In order to do this, your superclass needs to expose a designated initializer that takes va_list as a parameter. See vprintf for an example of how it is done in the standard library.

    va_list args;
    va_start(args, firstChild);
    id res = [self initWithChildren:firstChild varArg:args];
    va_end (args);
    return res;

- (id) initWithChildren:(NSObject*)firstChild, varArg:va_list args {
    // Do the actual initialization here
share|improve this answer
This is all discussed in the duplicate. :) –  rmaddy Feb 7 '13 at 3:45
@rmaddy I saw the dupe only after posting the answer. Unfortunately, SO does not show incoming dupes as you type the answer. –  dasblinkenlight Feb 7 '13 at 3:46
add comment
