如何用逗号分隔多个参数创建objc函数?

时间:2011-05-22 13:53:57

标签: objective-c nsarray

我想模仿[NSArray arrayWithObjects:]的功能,它允许我以这种方式输入争论:[MyClass doSomethingWithObjects: @"str1",@"str2",nil]。假设这是可能的,我该如何声明?

1 个答案:

答案 0 :(得分:12)

发现它在这里解释: 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);
}