Having some issues with the ... in ObjectiveC.
I’m basically wrapping a method and want to accept a nil terminated list and directly pass that same list to the method I am wrapping.
Here’s what I have but it causes an EXC_BAD_ACCESS crash. Inspecting the local vars, it appears when otherButtonTitles is simply a NSString when it is passed in with otherButtonTitles:@"Foo", nil]
+ (void)showWithTitle:(NSString *)title
message:(NSString *)message
delegate:(id)delegate
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(NSString *)otherButtonTitles, ...
{
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title
message:message
delegate:delegate
cancelButtonTitle:cancelButtonTitle
otherButtonTitles:otherButtonTitles] autorelease];
[alert show];
}
How do I simply siphon from the argument incoming to the argument outgoing, preserving the exact same nil terminated list?
You can’t do this, at least not in the way you’re wanting to do it. What you want to do (pass on the variable arguments) requires having an initializer on
UIAlertViewthat accepts ava_list. There isn’t one. However, you can use theaddButtonWithTitle:method:This is, of course, very problem-specific. The real answer is “you can’t implicitly pass on a variable argument list to a method/function that does not have a
va_listparameter”. You must therefore find a way around the problem. In the example you gave, you wanted to make an alertView with the titles you passed in. Fortunately for you, theUIAlertViewclass has a method that you can iteratively call to add buttons, and thereby achieve the same overall effect. If it did not have this method, you’d be out of luck.The other really messy option would be to make it a variadic macro. A variadic macro looks like this:
However, even with the variadic macro approach, you’d still need a custom macro for each time you wanted to do this. It’s not a very solid alternative.