Objective-C传递... nil个终止的参数列表 [英] Objective-C passing around ... nil terminated argument lists

查看:96
本文介绍了Objective-C传递... nil个终止的参数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ObjectiveC中的...遇到一些问题.

Having some issues with the ... in ObjectiveC.

我基本上是包装一个方法,并且想要接受一个nil终止列表,并将该列表直接传递给我要包装的方法.

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.

这就是我所拥有的,但它会导致EXC_BAD_ACCESS崩溃.检查本地变量时,当otherButtonTitles只是NSString且与otherButtonTitles:@"Foo", nil]

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];
}

如何简单地将传入的参数虹吸到传出的参数中,并保留完全相同的nil终止列表?

How do I simply siphon from the argument incoming to the argument outgoing, preserving the exact same nil terminated list?

推荐答案

您不能这样做,至少不能以您想要的方式进行.您要执行的操作(传递变量参数)要求在UIAlertView上有一个接受va_list的初始化程序.没有一个.但是,您可以使用addButtonWithTitle:方法:

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 UIAlertView that accepts a va_list. There isn't one. However, you can use the addButtonWithTitle: method:

+ (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:nil] autorelease];
    if (otherButtonTitles != nil) {
      [alert addButtonWithTitle:otherButtonTitles];
      va_list args;
      va_start(args, otherButtonTitles);
      NSString * title = nil;
      while(title = va_arg(args,NSString*)) {
          [alert addButtonWithTitle:title];
      }
      va_end(args);
    }

    [alert show];
}

当然,这是非常特定于问题的.真正的答案是您不能将变量参数列表隐式传递给没有va_list参数的方法/函数".因此,您必须找到解决问题的方法.在您给出的示例中,您想使用传入的标题制作一个alertView.幸运的是,UIAlertView类具有一种可以迭代调用以添加按钮的方法,从而实现了相同的总体效果.如果没有这种方法,您将很不走运.

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_list parameter". 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, the UIAlertView class 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:

#define SHOW_ALERT(title,msg,del,cancel,other,...) { \
  UIAlertView *_alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:del cancelButtonTitle:cancel otherButtonTitles:other, ##__VA_ARGS__] autorelease]; \
  [_alert show]; \
}

但是,即使采用可变参数宏方法,每次仍然需要自定义宏.这不是一个非常可靠的选择.

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.

这篇关于Objective-C传递... nil个终止的参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆