NSArray arrayWithObjects: if nil is meant to mark array end, can I do …nil, nil]?

前端 未结 4 2052
梦谈多话
梦谈多话 2021-01-03 07:40

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

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

as the first nil marks

4条回答
  •  再見小時候
    2021-01-03 07:56

    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.

    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.

提交回复
热议问题