为什么 UIActionSheet init 方法接受字符串数组而不是 NSString
这确实是一个新手问题,但它会帮助我更好地理解 Objective-c 是如何工作的。我在 iOS 应用程序中使用了 UIActionSheet。查看文档,这是相关的 init 方法:
- (id)initWithTitle:(NSString *)title delegate:(id < UIActionSheetDelegate >)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
其中 otherButtonTitles 据说是 NSString 的逗号分隔列表。在我看来,这对应于 NSArray,因此为了引起崩溃,我尝试了:
NSArray *buttons = [NSArray arrayWithObjects:@"B1",@"B2",nil];
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Actions" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete" otherButtonTitles:buttons];
然后显然应用程序因按钮 NSArray 而崩溃。 这听起来与 Java 可变参数非常相似,在类中你可以有类似的内容:
public void myMethod(String... param) {...};
对此方法的合法调用是:
myClass.myMethod("x");
myClass.myMethod("x","Y");
我的 iOS 应用程序中有很多使用 NSArray 的方法:
[myClass myMethod:[NSArray arrayWithObjects:....]];
这对我来说非常方便避免分配 NSArray 而是传递逗号分隔的 NSString 列表。我怎样才能做到这一点?我的意思是,从 myMethod 的角度来看,收到什么类型的参数以及应该如何考虑它?例如,我如何循环使用 NSString ???
谢谢
this is really a newbie question, but it would help me to have a better understanding of how Objective-c works. I have made use of UIActionSheet in a iOS app. Looking at documentation this is the relevant init method:
- (id)initWithTitle:(NSString *)title delegate:(id < UIActionSheetDelegate >)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
Where otherButtonTitles is said to be a comma separated list of NSString. In my mind this is correspondent to an NSArray, so with the intent of caushing a crash I tried:
NSArray *buttons = [NSArray arrayWithObjects:@"B1",@"B2",nil];
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Actions" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete" otherButtonTitles:buttons];
And then obviously the application crashed because of buttons NSArray.
This sounds so similiar to Java varargs, where in a class you can have something like:
public void myMethod(String... param) {...};
A legal call to this method is:
myClass.myMethod("x");
myClass.myMethod("x","Y");
I have a lot of methods in my iOS apps that made use of NSArray:
[myClass myMethod:[NSArray arrayWithObjects:....]];
And it would be very convenient for me to avoid alloc of NSArray but rather passing a comma separated list of NSString. How can I do that ? I mean, from the myMethod point of view, what type of param is received and how should it be considered ? For example how can I cycle trough an NSString ???
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
鉴于您的示例,以下内容应该有效:
它并不比听起来更复杂。 “逗号分隔的 NSString 列表”只不过是一个用逗号分隔的 NSString 列表。
Given your example, the following should work:
It's no more complicated than it sounds. A "comma-separated list of NSString" is nothing more than a list of NSStrings, separated by commas.
作为 Objective-c 新手之王,这里对我来说有一些误解。正如格雷厄姆所指出的,该方法确实使用了可变参数。乍一看我完全忽略了这一点,Java varargs 表示法在 Objective-c 中具有等效的:
事实上,如果你看一下 UIActionSheet 方法签名,它在有关其他按钮的部分中放置了完全相同的三点表示法:
也用于处理变量Objective-c 中的参数我发现了一个非常有用的链接:
http://cocoawithlove.com/2009/05/variable-argument-lists-in-cocoa.html
谈到我的问题,我可以通过实现“三个”来安全地重写我的所有方法点符号',并扔掉所有不必要的 NSArray。
As the king of Objective-c newbie as I am, here comes a little misundertanding for me. As pointed out by Graham, the method made indeed use of variable arguments. At first glance I completely missing this, the Java varargs notation has this equivalent in Objective-c:
Infact if you take a look at UIActionSheet method signature it put exactly the same three dot notation in the part regarding other buttons:
Also for dealing with variable arguments in objective-c I found a very useful link:
http://cocoawithlove.com/2009/05/variable-argument-lists-in-cocoa.html
Coming to my question, I can safely rewrite all my methods by implementing the 'three dot notation', and throw away all the unnecessary NSArray.