我想有一个方法,我可以放置尽可能多的参数,像我需要像NSArray:
- (id)initWithObjects:(id)firstObj,... NS_REQUIRES_NIL_TERMINATION;
我可以使用:
NSArray *array = [[NSArray alloc] initWithObjects:obj1,obj2,ob3,nil];
只要我在最后添加“nil”,我可以添加任意数量的对象来告诉我已经完成.
我的问题是我怎么知道有多少论证,我一次怎么通过他们呢?
解决方法
- (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); } }