2

How can I create a class with the initializer initWithObjects?

Or does it just make more sense to inherit from NSArray and work around it that way?

2 Answers 2

4

initWithObjects: is implemented using a C variable argument list. Here's an example implementation:

- (void)setContentByAppendingStrings:(NSString *)firstArg, ...
{
    NSMutableString *newContentString = [NSMutableString string];
    va_list args;
    va_start(args, firstArg);
    for (NSString *arg = firstArg; arg != nil; arg = va_arg(args, NSString*))
    {
        [newContentString appendString:arg];
    }
    va_end(args);

    [contents autorelease];
    contents = [newContentString retain];
}

See this page for more info.

Sign up to request clarification or add additional context in comments.

Comments

-3
@interface foo : NSObject {

    NSArray* objects;
}

-(id)initWithObjects:(NSArray*)array;

@end


@implementation foo

-(id)initWithObjects:(NSArray*)array{
    if(self = [super init]){
        objects = array;        
    }
    return self;
}
@end

1 Comment

You need to retain or copy the array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.