Ever wondered how the NSString's +stringWithFormat: method work? How you can pass endless arguments in it?
Well methods that take variable arguments are known as variadic methods.
Here is an example of such a method
Declare variable argument function in your .h file as below
- (void) appendObjects:(id) firstObject, ...; // This method takes a nil-terminated list of objects.
Here is the definition in .m file –
Stay tuned to my blog, twitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.
Well methods that take variable arguments are known as variadic methods.
Here is an example of such a method
Declare variable argument function in your .h file as below
- (void) appendObjects:(id) firstObject, ...; // This method takes a nil-terminated list of objects.
Here is the definition in .m file –
- (void) appendObjects:(id) firstObject, ...
{
id eachObject;
va_list argumentList;
if (firstObject) // The first argument isn't part of the varargs list,
{
[self addObject: firstObject];// so we'll handle it separately.
va_start(argumentList, firstObject); // Start scanning for arguments after firstObject.
while (eachObject = va_arg(argumentList, id)) // As many times as we can get an argument of type "id"
{
[self addObject: eachObject]; // that isn't nil, add it to self's contents.
}
va_end(argumentList);
}
}
Stay tuned to my blog, twitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.
No comments:
Post a Comment