wczekalski
wczekalski

Reputation: 745

Objective C method with multiple NSStrings as argument

I need to make init method with multiple NSStrings as argument.

Assume it looks like: '-(id) initWithSomething: (NSString *) things, nil;'

How to recognize number of strings and write them into array ?

Regards

Upvotes: 0

Views: 916

Answers (3)

Dair
Dair

Reputation: 16240

Use a variadic method:

//Interface
-(id) initWithSomething:(NSString*) arg1, ...;
//Implementation
-(id) initWithSomething:(NSString*) arg1, ... {
    va_list args;
    va_start(args, firstObject);
    id obj;
    for (obj = firstObject; obj != nil; obj = va_arg(args, id))
        //Do stuff with each object.
    va_end(args);
}

Upvotes: 4

PengOne
PengOne

Reputation: 48398

Basic objective-C syntax is as follows:

-(type)methodNameTakesInput:(type)param1 andMoreInput:(type)param2

So you can do

-(id)initWithString:(NSString *)str andOtherThing:(NSObject *)obj

Alternately, you could just pass the array you want:

-(id)initWithStuff:(NSArray *)arrayOfStuff

and fill the array as you normally would:

NSArray *arrayOfStuff = [NSArray arrayWithObjects:@"Strings!", @"More strings!", nil];

Upvotes: 0

Sean
Sean

Reputation: 5820

What you want is a Variadic function.

Upvotes: 3

Related Questions