带有输入数组的方法 [英] Method with an array of inputs
本文介绍了带有输入数组的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要一个方法,我可以像NSArray那样放置尽可能多的参数:
I want to have a method where I can put as many arguments as I need like the NSArray:
- (id)initWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION;
我可以使用:
NSArray *array = [[NSArray alloc] initWithObjects:obj1, obj2, ob3, nil];
我可以添加尽可能多的对象,只要我在末尾添加'nil'即可告诉它我已经完成了。
I can add as many objects as I want as long as I add 'nil' at the end to tell it I'm done.
我的问题是我怎么知道给出了多少论点,我将如何一次一个地通过它们?
My question is how would I know how many arguments were given, and how would I go through them one at a time?
推荐答案
- (void)yourMethod:(id) firstObject, ...
{
id eachObject;
va_list argumentList;
if (firstObject)
{
// do something with firstObject. Remember, it is not part of the variable argument list
[self addObject: firstObject];
va_start(argumentList, firstObject); // scan for arguments after firstObject.
while (eachObject = va_arg(argumentList, id)) // get rest of the objects until nil is found
{
// do something with each object
}
va_end(argumentList);
}
}
这篇关于带有输入数组的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文