Why does NSArray arrayWithObjects require a terminating nil?

前端 未结 6 840
时光取名叫无心
时光取名叫无心 2020-12-03 01:29

I understand that it marks the end of a set of varargs, but why can\'t it be implemented in such a way that doesn\'t require the nil?

6条回答
  •  孤街浪徒
    2020-12-03 02:28

    Any varargs function requires someway to know how many parameters are present -- if you don't nil terminate, you would just need something else. In this case the obvious alternative is a length, but then you'd need to update the length argument everytime you changed how many items were in the array, and that could be cumbersome, or worse broken.

    I am guessing you have an array that may contain nil?

提交回复
热议问题