5

I'm trying to extend a class that has a variadic method such as:

- (void)someMethod:(id)arguments, ... ;

and in the subclass override it by calling the original method like:

- (void)someMethod:(id)arguments, ... {
    [super someMethod:arguments, ...];

    // override implementation
    ...
}

but this doesn't work. Anyone know how to work this? Thanks.

justin
  • 104,054
  • 14
  • 179
  • 226
Taketo Sano
  • 499
  • 4
  • 14
  • So... Is this a category, or a superclass? Because Categories EXTEND and superclasses OVERSEE. – CodaFi Mar 05 '12 at 06:11
  • It's not possible generally to wrap a variadic function or method like this. – jscs Mar 05 '12 at 06:13
  • possible duplicate of [Objective-C passing around ... nil terminated argument lists](http://stackoverflow.com/questions/2345196/objective-c-passing-around-nil-terminated-argument-lists) – jscs Mar 05 '12 at 06:16
  • And see the question linked to in a comment there: http://stackoverflow.com/questions/150543/forward-an-invocation-of-a-variadic-function-in-c Also http://stackoverflow.com/questions/3143906/how-to-use-va-args-to-pass-arguments-on-variadic-parameters-elipsis – jscs Mar 05 '12 at 06:16
  • Thanks, you're right, it's actually written: "If you don't have a function analagous to vfprintf that takes a va_list instead of a variable number of arguments, you can't do it." – Taketo Sano Mar 05 '12 at 06:32

1 Answers1

3

similar to printf/vprintf, the base would declare:

- (void)someMethod:(id)arguments, ... ;

the subclass would implement:

- (void)vsomeMethod:(id)arguments vaList:(va_list)vaList;

then the base would just call vsomeMethod:vaList: in its implementation of someMethod:vaList:.

justin
  • 104,054
  • 14
  • 179
  • 226
  • Thanks, but in my case the base class cannot be modified. – Taketo Sano Mar 05 '12 at 06:27
  • 2
    @TaketoSano it would be just like implementing a variadic function, but you would forward the `va_list` to `vsomeMethod:vaList:` after creating it, rather than enumerating the arguments. since you cannot modify the base, this approach won't work for you because `someMethod:` must be defined by the base in order to forward properly. the only remaining 'workarounds' that I know of are implementation-defined. – justin Mar 05 '12 at 06:29
  • Great answer, though I might lead with "you can't." ;) – Dan Rosenstark Jan 03 '17 at 21:37