I’m trying to pass a variadic argument (NSString *) from one method to another method like the following example:
- (NSURL *)urlForId:(NSString *)aId params:(NSDictionary *)aParams parts:(NSString *)aPart, ... {
// ... do something with parts
}
- (NSURL *)specialUrlForId:(NSString *)aId params:(NSDictionary *)aParams parts:(NSString *)aPart, ... {
va_list arg;
va_start(args, aPart);
[self urlForId:aId params:aParam parts:args];
va_end(args);
}
The problems start when trying to pass args along. ARC complains about an implicit conversion from va_list (char *) to NSString *. I’ve tried everything to get this to work.
This same technique will work if i pass the va_list into [NSString stringWithFormat:…] so I don’t see why it isn’t working here.
Any help appreciated.
Your
urlForId:params:parts:method expects the first argument after theparts:keyboard to be anNSString*, but you’re passing it ava_list. Those are different types. Passing ava_listvariable as a function parameter doesn’t magically expand it into all of the original arguments. Under the covers ava_listis really just a pointer into your stack frame, and that’s all that gets passed.What you really need to do here is factor out the section of
urlForId:params:parts:that walks the parts list, and call that factored-out section directly fromspecialUrlForId:params:parts:. Presumably yoururlForId:params:parts:method looks something like this:So what you’d do here is move the part after
va_startand beforeva_endto a method that takes ava_listparameter:Then you make both
urlForId:params:parts:andspecialUrlForId:params:parts:call this newurlForId:params:arguments:method: