Objective-c - 将变量传递给可变长度方法
我有一个包含项目的数组,我想将它们传递给可变长度方法。你怎么做到的?
即,我有这个(例如):
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
-[UIAlertView initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:]
中的otherButtonTitles
参数的文档指出:你有没有尝试过这个:
The documentation for the
otherButtonTitles
parameter in-[UIAlertView initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:]
states that:Have you tried this:
您也可以在接受数组的函数中创建一个参数(简单的解决方案)
无论如何,...符号用于函数末尾的可变数量的参数。
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.