What they are called, as a rule, is “variational functions” (or, as it were, methods).
To create this, simply run your declartion method with , ... , as in
- (void)logMessage:(NSString *)message, ...;
At this point, you probably want to wrap it in a printf like function, since implementing one of them from scratch tries at best.
- (void)logMessage:(NSString *)format, ... { va_list args; va_start(args, format); NSLogv(format, args); va_end(args); }
Note the use of NSLogv , not NSLog ; consider NSLog(NSString *, ...); vs NSLogv(NSString *, va_list); or you need a string; initWithFormat:arguments: on NSString * .
If, on the other hand, you are not working with strings, but rather how
+ (NSArray *)arrayWithObjects:(id)object, ... NS_REQUIRES_NIL_TERMINATION;
everything becomes much easier.
In this case, instead of using the vprintf -style function, use a loop that goes through args , assuming id as you go, and parse them like you would in any loop.
- (void)logMessage:(NSString *)format, ... { va_list args; va_start(args, format); id arg = nil; while ((arg = va_arg(args,id))) {
This last example, of course, assumes that the va_args list is nil-terminated.
Note. . To do this, you may need to include <stdarg.h> ; but if the memory serves, this is included in the connection with NSLogv, that is, it happens with the help of "Foundation.h", therefore also "AppKit.h" and "Cocoa.h", as well as a number of others; therefore it should work out of the box.