itsaboutcode
itsaboutcode

Reputation: 25099

Purpose of "nil' at the end of Initializing Array in Objective-C

I have seen and done initialization of arrays and all put "nil" at the end of initialization but never question, why it is required to put there?

Plus if you are initializing your array in a loop, is it still necessary to put nil at the end of array? for example.

array = [[NSMutableArray alloc] init];

for (int i = 0 ; i < 10; i++)
{
   [array addObject:@"1"];
}

// now this line is required or not after i exit the loop?
[array addObject:nil];

Upvotes: 1

Views: 369

Answers (1)

Jacob Relkin
Jacob Relkin

Reputation: 163278

This concept is called nil-termination, and it's purpose is to provide a sentinel to the receiving function or method of where the variable argument list ends.

Upvotes: 6

Related Questions