0

Possible Duplicate:
NSArray creation with variable argument lists in Objective-C

that's it actually the question is very simple. Xcode suggests that the last item is "nil" and I was wondering why.

array1 = [[NSArray alloc] initWithObjects: @"one", @"two", @"three", @"four", nil];
Community
  • 1
  • 1
BanzaiTokyo
  • 1,376
  • 4
  • 17
  • 33
  • See: http://stackoverflow.com/questions/3602458/nsarray-creation-with-variable-argument-lists-in-objective-c – MarkPowell Jul 05 '11 at 21:15

2 Answers2

1

A null-termination (nil) character is used to determine the length of an array. Failure to properly terminate an array can result in buffer overflows and other undefined behavior.

George Johnston
  • 31,652
  • 27
  • 127
  • 172
0

Aside from redirecting to the docs about NSArray initWithObjects, it is worth noting that in general, with variadic functions (i.e. functions that take a variable number of arguments), either you specify the number of arguments upfront, or you terminate the argument list in some way. Nil is a good way of terminating the argument list and is the approach taken with initWithObjects.

sergio
  • 68,819
  • 11
  • 102
  • 123