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 Objective-C Basics (Retired) Foundation Framework NSArray

Harry Stromfelt
Harry Stromfelt
2,985 Points

Are we adding char's as the array input?

When we add 'bravo' to the NSArray, does it get stored as one long string, or does the array in fact break into {{b,r,a,v,o},{etc,,,}}?

Hope this makes sense!!

5 Answers

It gets stored as a string literal, which is an object in Objective-C;

If you were in C then, yes, it would get stored as an array of chars.

That is what *argv[] is in main() of a C program, which can also be written as **argv because it is a pointer to a pointer. The first pointer points to the string in the array and the second pointer points to the first character of that string. And the reason the pointer points to the first character of the string is because the compiler will start at that character and increment down the array of characters, the string, until it hits a null character, which is automatically added by the compiler when you initialize the string as a literal in C. However, if you don't, and then forget to put a null character at the end, the pointer will keep going and you will get funky output because it's pointing to memory addresses outside the scope of where that string is stored in memory.

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

I'm not quite sure what you mean, but I think you write Bravo, not B,r,a,v,o.

Harry Stromfelt
Harry Stromfelt
2,985 Points

Thanks for the reply!

I'll try to clarify: I know that arrays can be multidimensional in some cases, so each index position breaks down into more input points. So rather than having the example NSArray *lots = {"bravo", "charlie"}; it is really stored char by char as NSArray *lots = {{"b","r","a","v","o"},{"c","h","a","r","l","i","e"}};

I haven't done a lot of Objective c, so I will have seen this somewhere else I think. I am just wondering how it is the Array class chooses what type it is storing, you don't need to tell it that one entry is a string, the next is a single char, the next may then be an integer; It seems to be doing the assessing itself..

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

Okay, I don't know that much either so hopefully a more expirianced person will come along:^)

Harry Stromfelt
Harry Stromfelt
2,985 Points

fingers crossed but thanks anyway!