Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make objc functions with comma separated multiple arguements?

I would like to imitate the functionality of [NSArray arrayWithObjects:] which allows me to type in arguements this way: [MyClass doSomethingWithObjects: @"str1",@"str2",nil]. Assuming this is possible, how can I declare this?

like image 443
Alex Gosselin Avatar asked May 22 '11 13:05

Alex Gosselin


1 Answers

Found it explained here: http://cocoawithlove.com/2009/05/variable-argument-lists-in-cocoa.html

//interface:
 - (void)foo:(NSString *)firstString, ... NS_REQUIRES_NIL_TERMINATION;

//implementation:
 - (void)foo:(NSString *)firstArg, ...
 {
    va_list args;
    va_start(args, firstArg);
    for (NSString *arg = firstArg; arg != nil; arg = va_arg(args, NSString*))
    {
        [self bar:arg];
    }
    va_end(args);
}
like image 131
Alex Gosselin Avatar answered Oct 25 '22 09:10

Alex Gosselin