为什么 UIActionSheet init 方法接受字符串数组而不是 NSString

发布于 2024-11-26 07:37:13 字数 1212 浏览 1 评论 0原文

这确实是一个新手问题,但它会帮助我更好地理解 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 技术交流群。

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

发布评论

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

评论(2

倾城月光淡如水﹏ 2024-12-03 07:37:13

鉴于您的示例,以下内容应该有效:

UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Actions" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete" otherButtonTitles:@"B1",@"B2",nil];

它并不比听起来更复杂。 “逗号分隔的 NSString 列表”只不过是一个用逗号分隔的 NSString 列表。

Given your example, the following should work:

UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Actions" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete" otherButtonTitles:@"B1",@"B2",nil];

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.

少跟Wǒ拽 2024-12-03 07:37:13

作为 Objective-c 新手之王,这里对我来说有一些误解。正如格雷厄姆所指出的,该方法确实使用了可变参数。乍一看我完全忽略了这一点,Java varargs 表示法在 Objective-c 中具有等效的:

public void myMethod(String... var);
-(void)myMethod:(NSString*)var,...;

事实上,如果你看一下 UIActionSheet 方法签名,它在有关其他按钮的部分中放置了完全相同的三点表示法:

otherButtonTitles:(NSString *)otherButtonTitles, ...

也用于处理变量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:

public void myMethod(String... var);
-(void)myMethod:(NSString*)var,...;

Infact if you take a look at UIActionSheet method signature it put exactly the same three dot notation in the part regarding other buttons:

otherButtonTitles:(NSString *)otherButtonTitles, ...

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.

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