why when you initialize array using initWithObjects the last item is "nil"? [duplicate]
Possible Duplicate:
NSArray creation with variable argument lists in Objective-C
th开发者_StackOverflow中文版at'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];
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.
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
.
精彩评论