使用可变参数列表创建 NSArray [英] NSArray creation with variable argument lists

查看:19
本文介绍了使用可变参数列表创建 NSArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个数组便捷方法采用以nil结尾的以逗号分隔的对象列表.

This array convenience method takes a comma-separated list of objects ending with nil.

myArray = [NSArray arrayWithObjects:aDate, aValue, aString, nil];

nil 的目的是什么?

推荐答案

空终止变量参数列表,或 va_lists,继续遍历参数列表,直到遇到占位符或哨兵,nil.

Null terminated variable argument lists, or va_lists, keep walking the list of arguments until they encounter a placeholder or sentinel, which is nil.

由于该方法无法知道您传递了多少个参数,因此它需要哨兵 (nil) 来判断列表的结束位置.

Since the method has no way of knowing how many arguments you are passing, it needs the sentinel (nil) to tell where the list ends.

这篇关于使用可变参数列表创建 NSArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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