Overriding a variadic method in objective-c

拈花ヽ惹草 提交于 2019-12-12 08:26:28

问题


When subclassing in objective-c, how can I forward a call to the superclass in the case of a variadic method. By what should I replace the ??? below to send all the objects I got?

- (void) appendObjects:(id) firstObject, ...
{
   [super appendObjects: ???];
}

回答1:


You can't. To safely pass all the variadic arguments, you need a method to accept a va_list.

In super,

-(void)appendObjectsWithArguments:(va_list)vl {
  ...
}

-(void)appendObject:(id)firstObject, ...
  va_list vl;
  va_start(vl, firstObject);
  [self appendObjectsWithArguments:vl];
  va_end(vl);
}

And use [super appendObjectsWithArguments:vl] when you override the method in the subclass.




回答2:


There is a good article on this at Cocoa with Love: Variable argument lists in Cocoa




回答3:


Try this:

- (void) appendObjects:(id) firstObject, ...
{
   va_list args = &firstObject;
   [super appendObjects: args];
}

If that doesn't do the trick, read the manual pages on varargs.



来源:https://stackoverflow.com/questions/3987209/overriding-a-variadic-method-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!