开发者

why when you initialize array using initWithObjects the last item is "nil"? [duplicate]

开发者 https://www.devze.com 2023-03-17 06:41 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: NSArray creation with variable argument lists in Objective-C
This question already has answers here: Closed 11 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号