Re: Nil-terminated object list?
Re: Nil-terminated object list?
- Subject: Re: Nil-terminated object list?
- From: "b.bum" <email@hidden>
- Date: Mon, 19 Jul 2004 10:24:36 -0700
On Jul 19, 2004, at 9:41 AM, Nick Zitzmann wrote:
How does one make a method that takes a nil-terminated object list as
an argument, like NSArray's +arrayWithObjects: and NSDictionary's
+dictionaryWithObjectsAndKeys: methods?
Situation: I'd like to write an instance method in an NSMutableArray
category that adds a nil-terminated object list to an existing
NSMutableArray, but I can't find any documentation or examples on how
to do this. I've searched and found some marg methods in the
Objective-C runtime, but I can't figure out how to use them... Does
anyone know how to do this, and if so, how?
You need to use a va_list. Pseudo code:
- (void) foo: object1, ...
{
va_list args;
va_start(args, object1);
id anObject;
while (anObject = va_arg(args, id)) {
... do something with anObject here ...
}
va_end(args);
}
Note that va_arg() takes the TYPE of the next argument in the arglist
as the second argument. So, for something like stringWithFormat: or
fprintf(), the first argument contains the type information (in the
form of %d %s %@ style indicators) for the arguments passed in the
varargs list.
Very fragile, but handy nonetheless.
b.bum
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.