Objective-c - 将变量传递给可变长度方法

发布于 2024-08-25 06:59:01 字数 392 浏览 6 评论 0原文

我有一个包含项目的数组,我想将它们传递给可变长度方法。你怎么做到的?

即,我有这个(例如):

NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];

[[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:[array objectAtIndex:0] otherButtonTitles:[array objectAtIndex:1], [array objectAtIndex:2], nil];

但是想象一下该数组可以有可变长度的项目,所以你不能像这样对其进行硬编码。

I've got an array with items, and I want to pass these in to a variable-length method. How do you do that?

I.e., I've got this (for example):

NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];

[[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:[array objectAtIndex:0] otherButtonTitles:[array objectAtIndex:1], [array objectAtIndex:2], nil];

But imagine that array could have a variable length of items, so you cant hardcode it like this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

穿越时光隧道 2024-09-01 06:59:01

-[UIAlertView initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:] 中的 otherButtonTitles 参数的文档指出:

使用此参数相当于使用此标题调用 addButtonWithTitle: 来添加更多按钮。

你有没有尝试过这个:

NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:nil];
for (NSString *s in array) {
    [view addButtonWithTitle:s];
}

The documentation for the otherButtonTitles parameter in -[UIAlertView initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:] states that:

Using this argument is equivalent to invoking addButtonWithTitle: with this title to add more buttons.

Have you tried this:

NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:nil];
for (NSString *s in array) {
    [view addButtonWithTitle:s];
}
三生一梦 2024-09-01 06:59:01
- (id) initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles ...
{
    va_list args;
    va_start(args, otherButtonTitles);
    for (NSString *arg = otherButtonTitles; arg != nil; arg = va_arg(args, NSString*))
    {
        //do something with nsstring
    }
    va_end(args);
}

您也可以在接受数组的函数中创建一个参数(简单的解决方案)

无论如何,...符号用于函数末尾的可变数量的参数。

- (id) initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles ...
{
    va_list args;
    va_start(args, otherButtonTitles);
    for (NSString *arg = otherButtonTitles; arg != nil; arg = va_arg(args, NSString*))
    {
        //do something with nsstring
    }
    va_end(args);
}

You could also just make an argument in your functions that accepts an array (easy solution)

Anyway the ... notation is for a variable amount of arguments at the end of a function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文