Andrew M.
Andrew M.

Reputation: 58

How to implement initWithObjects?

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?

Upvotes: 2

Views: 485

Answers (2)

Darren
Darren

Reputation: 25619

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.

Upvotes: 4

echo
echo

Reputation: 7845

@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

Upvotes: -3

Related Questions