NSArray arrayWithObjects:如果nil是要标记数组结尾,我可以... nil,nil]吗? [英] NSArray arrayWithObjects: if nil is meant to mark array end, can I do ...nil, nil]?

查看:93
本文介绍了NSArray arrayWithObjects:如果nil是要标记数组结尾,我可以... nil,nil]吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果nil用来标记参数的结尾,那么我可以使用:

If nil is meant to mark the end of parameters, then can I use:

[NSArray arrayWithObjects:obj1, obj2, nil, nil, nil];

第一个nil标记数组结尾,之后的两个nil会被忽略吗?

as the first nil marks array end and two nils after will be ignored?

我得到两个相反的答案,并将尝试一下.

I got two opposite answers and will try it out.

更新:

我需要执行此操作的原因是,我需要创建一个UIAlertView,该UIAlertView可能具有以下按钮:仅确定",或呼叫"和取消",因此是确定"或取消"取消按钮,而在某些情况下呼叫"是其他按钮,在其他情况下,我不需要任何其他按钮,这就是为什么我需要这样做.

The reason why I need to do this, is I need to create an UIAlertView, which may have buttons: 'OK' only, or 'Call' and 'Cancel', so either 'OK' or 'Cancel', is the cancel button, whereas 'Call' is other buttons in one case, in other cases, I do not need any other buttons, that's why I need to do this.

推荐答案

在末尾添加nil并不是要向数组中添加nil,它只是C语言如何处理...变量参数列表的产物.与NSArray或NSMutableArray无关,您都不能在其中存储nil.

The addition of nil to the end is not intended to add nils to an array, its simply an artifact of how C processes ... variable argument lists. It has nothing to do with NSArray or NSMutableArray, you cannot store nil in either.

因此,编译器是否接受nil,nil,nil实际上无关紧要.编译器将在第一个nil处停止读取.并且首先编写该代码表明对obj C集合和var arg方法有误解.

So whether the compiler accepts, nil, nil, nil is actually irrelevant. The compiler will stop reading at the first nil. And writing that code in the first place shows a misunderstanding of obj C collections and var arg methods.

为什么不使用新的文字语法而只说

Why not use the new literal syntax and just say

NSArray *myArray = @[@"bla", @"bla", @"bla"];

无论哪种方式,多余的内容都与您提供的语法无关.

Either way the extra nils matter not in the syntax you provided.

这篇关于NSArray arrayWithObjects:如果nil是要标记数组结尾,我可以... nil,nil]吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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