How to create variable argument methods in Objective-C
NickName:Chris Rutkowski Ask DateTime:2011-01-26T20:41:12

How to create variable argument methods in Objective-C

Maybe this will be obviously simple for most of you, but could you please give an example how to create similar methods (in Objective-C) and functions in C to create functions like NSString's stringWithFormat:, or NSLog().

Just to remind:

[NSString stringWithFormat:@"example tekst %i %@ %.2f", 122, @"sth", 3.1415"];
NSLog(@"account ID %i email %@", accountID, email);

I'd like to create the similar to NSString's method stringWithFormat:, NSURL - urlWithFormat.

Copyright Notice:Content Author:「Chris Rutkowski」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/4804674/how-to-create-variable-argument-methods-in-objective-c

Answers
Williham Totland 2011-01-26T12:56:56

What these are called, generally, is \"variadic functions\" (or methods, as it were).\n\nTo create this, simply end your method declartion with , ..., as in \n\n- (void)logMessage:(NSString *)message, ...;\n\n\nAt this point you probably want to wrap it in a printf-like function, as implementing one of those from scratch is trying, at best.\n\n- (void)logMessage:(NSString *)format, ... {\n va_list args;\n va_start(args, format);\n NSLogv(format, args);\n va_end(args);\n}\n\n\nNote the use of NSLogv and not NSLog; consider NSLog(NSString *, ...); vs NSLogv(NSString *, va_list);, or if you want a string; initWithFormat:arguments: on NSString *.\n\n\n\nIf, on the other hand, you are not working with strings, but rather something like\n\n+ (NSArray *)arrayWithObjects:(id)object, ... NS_REQUIRES_NIL_TERMINATION;\n\n\nthings get a lot easier.\n\nIn that case, instead of a vprintf-style function, use a loop going through args, assuming id as you go, and parse them as you would in any loop.\n\n- (void)logMessage:(NSString *)format, ... {\n va_list args;\n va_start(args, format);\n\n id arg = nil;\n while ((arg = va_arg(args,id))) {\n /// Do your thing with arg here\n }\n\n va_end(args);\n}\n\n\nThis last sample, of course, assumes that the va_args list is nil-terminated.\n\nNote: In order to make this work you might have to include <stdarg.h>; but if memory serves, this gets included in connection with NSLogv, meaning it comes down by way of \"Foundation.h\", therefore also \"AppKit.h\" and \"Cocoa.h\", as well as a number of others; so this should work out of the box.",


M-frankied 2011-01-26T12:47:51

- (void)methodWithFormat:(NSString*)format, ... {\n va_list args;\n va_start(args,format);\n //loop, get every next arg by calling va_arg(args,<type>)\n // e.g. NSString *arg=va_arg(args,NSString*) or int arg=(args,int)\n va_end(args);\n}\n\n\nIf you want to pass the variable arguments to stringWithFormat:, use something like:\n\nNSString *s=[[[NSString alloc] initWithFormat:format arguments:args] autorelease];\n",


More about “How to create variable argument methods in Objective-C” related questions

How to create variable argument methods in Objective-C

Maybe this will be obviously simple for most of you, but could you please give an example how to create similar methods (in Objective-C) and functions in C to create functions like NSString's

Show Detail

How to create variable argument methods in Objective-C

Maybe this will be obviously simple for most of you, but could you please give an example how to create similar methods (in Objective-C) and functions in C to create functions like NSString's

Show Detail

argument types of undocumented Objective-C methods in Instruments (OSX)

Is there a way to determine the argument types of Objective-C methods traced by Instruments? I created a custom DTrace Instrument that just lists all Objective-C calls in a class. I am trying to sw...

Show Detail

Methods with variable argument list

I am preparing for SCJP and I came to know Methods with variable argument list. I have couple questions. What is "Methods with variable argument list". When to use "Methods with variable argument ...

Show Detail

How are Objective-C factory methods converted into Swift convenience initializers?

Apple's documentation is quite clear about how Objective-C initialization methods get converted into Swift intializers: The “init” prefix gets sliced off and becomes a keyword to indicate that the

Show Detail

Using struct with Objective-C methods

I'm writing a simple game, and thought it would be much easier to use structures. However, I can't declare methods that need the structs. How could I use a struct as an argument to an Objective-C ...

Show Detail

Good practice for disambiguating argument names versus instance variable names in Objective-C

I run into a fairly common scenario in Objective-C where I pass in a variable to an init method and then want to assign it to an instance variable of the same name. However I have not found a way to

Show Detail

Objective C : Cocoa : 2 sets of variable arguments?

It is possible to have a method in this manner: [obj mergeObjs:obj1,obj2,obj3,nil]; Or have a method in this manner: [obj mergeObjs:obj1,obj2...obj(n),nil usingBlocks:blk1,blk2,blk3....blk(m),n...

Show Detail

Objective-C Methods Referencing

I know how to write selector names in Objective-C like mergeThis:withThat:, but can somebody tell me, how can I reference (e.g. in documentation, text or commit message) that a method belongs to a ...

Show Detail

Invoking methods reflectively with block arguments

I am working on an abstraction layer for making calls from JavaScript within a UIWebView into the native part of my application. To that end I require a generic mechanism which translates a JavaSc...

Show Detail