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 trialmalcolm parrish
2,093 PointsWhy does the array have to end with nil?
No details
4 Answers
Alan Johnson
7,625 PointsMy 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.
Vladimir Cezar
7,710 PointsYou can use a different syntax that does not require nil. Like so:
NSArray *arrayName = @[item1, item2, item3];
Stephen Whitfield
16,771 PointsCorrect. This is called Objective-C Literals and is briefly covered here.
Michael Bowen
770 PointsYou 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
5,648 Pointswhats the difference between null and nill ?