Getting EXC_BAD_ACCESS with va_list

Following the sample in the article http://cocoawithlove.com/2009/05/variable-argument-lists-in-cocoa.html , I wrote some special handling of variable argument methods to forward them to another method.

- (void) someMethod:(NSString *)name
   wittParamsAndKeys:(id)firstParam, ... {

va_list args;
va_start(args, firstParam);
NSDictionary* paramsAndKeys = 
    [[NSDictionary alloc] initWithObjectsAndKeys:firstParam, args, nil];
va_end(args);

}

But I get EXC_BAD_ACCESS . So, I tried to remove nilfrom the arguments in NSDictionary:

NSDictionary* paramsAndKeys = 
    [[NSDictionary alloc] initWithObjectsAndKeys:firstParam, args];

Again an exception. Now I have an exception initWithObjectsAndKeys:for invalid parameters.

I am wondering if there is some way to simply forward the arguments of a variable to another method?

+3
source share
2 answers

See this question: Variadic list parameter

, . :

        NSMutableArray* values = [NSMutableArray arrayWithObject: first_param];
        NSMutableArray* keys = [NSMutableArray array];
        va_list args;
        va_start(args, t1);
        id arg;
        int i = 0;
        while ( ( arg = va_arg( args, id) ) != nil ) {
            if( (++i)%2 )
                [values addObject: arg];
            else
               [keys addObject: arg];
        }

NSDictionary* dict = [NSDictionary dictionaryWithObjects: values forKeys: keys];
+4

, - ?

- .

/ ( ).

+2

All Articles