I noticed this in Joe Hewitt's source for Three20 and I've never seen this particular syntax in Objective-C before. Not even sure how to reference it in an appropriate Google search.
我在Joe Hewitt的Three20源代码中注意到了这一点,之前我从未在Objective-C中看到过这种特殊的语法。甚至不确定如何在适当的Google搜索中引用它。
From TTTableViewDataSource:
来自TTTableViewDataSource:
+ (TTSectionedDataSource*)dataSourceWithObjects:(id)object,... {
The "..." is what's throwing me off here. I'm assuming it's a form of enumeration where a variable amount of arguments may be supplied. If it is, what's the official name for this operator and where can I reference the documentation for it?
“...”就是把我从这里扔掉的原因。我假设它是一种枚举形式,可以提供可变数量的参数。如果是,这个运营商的官方名称是什么,我在哪里可以参考它的文档?
Thank you kindly.
非常感谢你。
1 个解决方案
#1
40
It's a variadic method, meaning it takes a variable number of arguments. This page has a good demonstration of how to use it:
它是一种可变方法,意味着它需要可变数量的参数。这个页面很好地演示了如何使用它:
#import <Cocoa/Cocoa.h>
@interface NSMutableArray (variadicMethodExample)
- (void) appendObjects:(id) firstObject, ...; // This method takes a nil-terminated list of objects.
@end
@implementation NSMutableArray (variadicMethodExample)
- (void) appendObjects:(id) firstObject, ...
{
id eachObject;
va_list argumentList;
if (firstObject) // The first argument isn't part of the varargs list,
{ // so we'll handle it separately.
[self addObject: firstObject];
va_start(argumentList, firstObject); // Start scanning for arguments after firstObject.
while (eachObject = va_arg(argumentList, id)) // As many times as we can get an argument of type "id"
[self addObject: eachObject]; // that isn't nil, add it to self's contents.
va_end(argumentList);
}
}
#1
40
It's a variadic method, meaning it takes a variable number of arguments. This page has a good demonstration of how to use it:
它是一种可变方法,意味着它需要可变数量的参数。这个页面很好地演示了如何使用它:
#import <Cocoa/Cocoa.h>
@interface NSMutableArray (variadicMethodExample)
- (void) appendObjects:(id) firstObject, ...; // This method takes a nil-terminated list of objects.
@end
@implementation NSMutableArray (variadicMethodExample)
- (void) appendObjects:(id) firstObject, ...
{
id eachObject;
va_list argumentList;
if (firstObject) // The first argument isn't part of the varargs list,
{ // so we'll handle it separately.
[self addObject: firstObject];
va_start(argumentList, firstObject); // Start scanning for arguments after firstObject.
while (eachObject = va_arg(argumentList, id)) // As many times as we can get an argument of type "id"
[self addObject: eachObject]; // that isn't nil, add it to self's contents.
va_end(argumentList);
}
}