Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Simple iPhone App (iOS7) Creating a Data Collection Implementing an Array

Why does the array have to end with nil?

No details

4 Answers

Alan Johnson
Alan Johnson
7,625 Points

My understanding is that it's because Objective-C needs to know when the list ends for variable length arguments. I'm not sure why it can't detect it in its syntax, but it looks like that's the reason.

There's a interesting discussion here about it.

 Vladimir Cezar
 Vladimir Cezar
7,710 Points

You can use a different syntax that does not require nil. Like so:

NSArray *arrayName = @[item1, item2, item3];

Correct. This is called Objective-C Literals and is briefly covered here.

You need a nil because the pointer to the array points at the first element in the array and will keep traversing the array until it hits a nil, then it knows that is the end of the array.

If there is not a nil then the pointer will continue into arbitrary memory addresses.

This is the same concept of strings using null characters in C.

Shaun Kelly
Shaun Kelly
5,648 Points

whats the difference between null and nill ?