加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 具有输入数组的方法

发布时间:2020-12-16 05:34:11 所属栏目:百科 来源:网络整理
导读:我想有一个方法,我可以放置尽可能多的参数,像我需要像NSArray: - (id)initWithObjects:(id)firstObj,... NS_REQUIRES_NIL_TERMINATION; 我可以使用: NSArray *array = [[NSArray alloc] initWithObjects:obj1,obj2,ob3,nil]; 只要我在最后添加“nil”,我可
我想有一个方法,我可以放置尽可能多的参数,像我需要像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);
  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读