Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Object-Oriented Objective-C Tying it All Together Categories

Ryan Jin
Ryan Jin
15,337 Points

Isn't NSArray immutable?

Isn't NSArray immutable, or unchangeable. If so, why in the video, Gabe could do something like

NSArray *movies = @["Multiple movies in this array..."];
movies = [movies deduped];

4 Answers

Ruggiero A
Ruggiero A
8,534 Points

Yes NSArray are immutable. Keep in mind that a NSArray object can't change, alias its element can't change nor its dimension. So you can't do a thing like

[movies addObject:obj];

cause you need a NSMutableArray for that. But when you do

movies = [movies deduped];

you're not changing the object, you're changing the reference of the object that the variable movies keeps. Keep in mind that movies is not a NSArray itself, but a pointer to NSArray, and it's not a constant pointer. This means that the function deduped in movies, creates another NSArray and returns it, so when you're writing that line, you're creating and assigning to movies a new array.

Ryan Jin
Ryan Jin
15,337 Points

So does that mean the pointer is mutable, but the NSArray is immutable? If so, doesn't this take up more memory while a pointer is meant to use less memory?

Ruggiero A
Ruggiero A
8,534 Points

Yeah the pointer is mutable, it would be different if you were declaring it as static const. It doesn't take up more memory cause a pointer keeps a simple 4-bytes or 8-bytes value (a RAM address), it does not change much if it's mutable or not. NSMutableArrays tend to use more memory because the size of elements is variable, so the allocation is not optimized and they tend to use more memory than they would need.

Ryan Jin
Ryan Jin
15,337 Points

Does it that dedupe method delete the original NSArray and create a new one? If not, doesn't that make it very inefficient?

Ruggiero A
Ruggiero A
8,534 Points

The method provides a NSArray allocation and initialization. About the destruction, that's up to ARC (assuming it's enabled)

Ryan Jin
Ryan Jin
15,337 Points

So if there is no that auto release pool thing in the code, there are basically two NSArrays when you call that deduped method?

Ruggiero A
Ruggiero A
8,534 Points

Probably the function should release the array after creating its copy.. I can't exactly answer here because I never programmed in Objective-C with no ARC (ARC is up since late 2010). Anyway most libraries use ARC, so it's best you use it in your projects. If a library doesn't use it, you can disable ARC for that library itself.

Ryan Jin
Ryan Jin
15,337 Points

So the point is there are two NSArrays, is that right? And deleting the first one to conserve memory is being handled by the ARC?

Ruggiero A
Ruggiero A
8,534 Points

Yes, the presence of two NSArrays though is very temporary, that's not gonna slow down your apps! ;D

Ruggiero A
Ruggiero A
8,534 Points

Oops I just noticed I answered 2 more times instead of comment... Sorry I'm not used to this forum

Naomi Schettini
Naomi Schettini
1,608 Points

How did the pointer know to point at NSArray movies??? That is where I am so confused in ObjC and I think it would help if I understood some of the mechanics behind the compiler..

Ruggiero A
Ruggiero A
8,534 Points

NSArray *movies isn't a variable of type NSArray. Actually it is a variable pointer TO NSArray. In Objective-C there is no way to actually assign to a variable the value of an object (so NSArray movies is just illegal, unlike C++). For this way, the proper value of movies is not an array, or an object, but it is an address of memory. When you call it methods, like [movies firstObject], you actually send a message to the object that movies points to. Objects are allocated in memory and variables or constants are just reference to them. (For this reason when no variable is pointing at an object in memory, it gets de-allocated by ARC)

Said this, the NSArray object allocated in memory, is IMMUTABLE. That means that you cannot modify its content, and above all you cannot modify its size. What you can do, since a basic NSArray* variable is just a variable pointer (just as a basic int* variable), it can change value-address (the value of a pointer is an address).

When you do movies = [movies deduped];, the deduped function is creating another object for you, which not depends on movies itself, but is a modified copy of it. After the assignment, movies points to a different object, while the old object which movies used to point to, gets de-allocated by ARC. If ARC was disabled, the disallocation should have happened in deduped function.