4

Would someone be so kind as to confirm that I am understanding this correctly. My initial response to this was that the mutableArray when assigned to an immutableArray would become an immutableArray, however this is not the case.

Is this because the initial array is allocated as a mutableArray and the immutableArray is just assigned to point to the same object. The compiler gives a warning, but the the code executes just fine at runtime.

NSMutableArray *mArray = [NSMutableArray arrayWithObjects:@"Teddy", @"Dog", nil];
NSArray *iArray = mArray;
[iArray addObject:@"Snoss"]; // Normally this would crash NSArray

much appreciated

gary.

4 Answers 4

5

Don't confuse the physical object that you just created, with how you are effectivley casting it in your code.

In this case, you did physically create a NSMutableArray.

Then you effectivley cast it to an NSArray - which is totally valid - for example, there are many cases where a function might want an NSArray, and you can pass it an NSArray, or anything derived from it (like an NSMutableArray).

The problem is that you get a compiler warning because you are trying to call addObject on an object which the compiler thinks is just an NSArray because that's what it's physical type is.

This will actually work, because it actually is an NSMutableArray, and in runtime would respond to that selector.

It's bad coding practice to do it this way, because NSArray doesn't actualy respond to the addObject selector. For example, I could create a function, like:

-(void) addIt:(NSArray)myThing {
  [myThing addObject:@"New String"];
}

The compiler would give you a warning saying the "NSArray" doesn't respond to the "addObject" selector. If you cast an NSMutableArray, and passed it to this function, it myThing, it would actually work.

It's bad practice though, because if you actually passed an NSArray it would crash.

Summary: Don't confuse what the object really is vs. what you are making the compiler interpret it as.

Sign up to request clarification or add additional context in comments.

Comments

2

Yes, you are right. You should do

NSArray *array = [NSArray arrayWithArray:mutableArray];

to ensure that nobody can change the array

Comments

1

Your iArray is actually referencing to NSMutableArray instance, that's why it is a NSMutableArray.

Obj-c doesn't have strict check on class types, all objects are of type 'id'. You could write

NSNumber *iArray = mArray

Compiler will show a warning of wrong cast (not error). But it will work.

Don't mess with pointers, there is no object type transformations as you can expect in C++. (there are overloadable operators for casting object to another class).

Obj-c works with objects much like script/interpreted languages. Dynamic typing (objects only), reflection, dynamic change of methods of instance of classes etc - full flexibility. A perfect mix of speed of low-level C/C++ and flexibility of dynamism.

Comments

0

AFAIK, you are correct. NSMutableArray is a subclass of NSArray, so you can assign mArray to iArray here without a problem.

However, it isn't clean code and you should avoid it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.