Re: no fixed variable parameter preceding a variable ... parameter?
Re: no fixed variable parameter preceding a variable ... parameter?
- Subject: Re: no fixed variable parameter preceding a variable ... parameter?
- From: Ben Dougall <email@hidden>
- Date: Sun, 26 Oct 2003 14:02:38 +0000
here's how to pass and extract a variable number of objects without
using an array to pass them, jic anyone's interested:
calling example:
TestClass *test = [[TestClass alloc] init];
NSString *anObject = [[NSString alloc] initWithString:@"any type"];
NSString *another = [[NSString alloc] initWithString:@"of objects"];
[test passVariableNumberOfObjects: anObject, another, @"and another",
nil]; // must be objects and list terminated with nil
#import "TestClass.h"
@implementation TestClass
- (void)passVariableNumberOfObjects:(id)firstObject, ...
{
NSMutableArray *passedObjects;
if( firstObject != NULL ) {
id tmp;
passedObjects = [[NSMutableArray alloc] init];
[passedObjects addObject: firstObject ];
va_list arglist;
va_start(arglist, firstObject);
while( tmp = va_arg(arglist, id) )
[passedObjects addObject: tmp ];
va_end(arglist);
// can do whatever with passed objects which are in the passedObjects
array now
NSLog(@"%@", passedObjects);
[passedObjects release];
} else
NSLog(@"no objects passed");
}
@end
_______________________________________________
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.