0

I'm new into Obj-C.

I would like to know will there be any problems if I assigning NSMutableArray to NSArray as like example code below:

NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:@"item1", @"item2", nil];
NSArray *array = mutableArray;

I know that if i use [mutableArray copy] it will duplicate the array as new set. My question is will my approach conflict with any guidance or will have any problems?

Thanks.

3 Answers 3

4

Both pointers will refer to the same object. The nature and behavior of that object will remain the same. It is not aware nor affected by which pointers refer to it.

If the array is mutated, those mutations will be observable regardless of which pointer you use to message or examine the array.

The compiler will complain if methods present in NSMutableArray but not NSArray are sent via the array pointer. However, if you ignore the compiler's complaints, the methods will still work as normal. The static types of the pointers is only significant at compile time. They are not represented in the compiled program (except, possibly, in debugging information, but that doesn't affect runtime behavior).

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

2 Comments

I assume most of the mutable class will be as same behavior? Anyway, Thank you for the detailed and easy to understand explanation! :)
All objects of any class have the same behavior. The type of the pointer doesn't affect anything except the compiler's warnings. You shouldn't do this, but you could do NSString* foo = (NSString*)mutableArray. You've told the compiler that the pointer points to an NSString when it actually points to an NSMutableArray. If you then send foo messages that NSMutableArray implements, they will work as normal. The compiler will complain for methods that NSString doesn't declare (e.g. -count) but not for those it does (e.g. -description). Sending -length will cause an exception.
1

The catch is this:

if mutableArray changed somehow, and the array still think of it self immutable.
Then, things might get out of control.

Other than this, I don't see much problem here...

3 Comments

Thanks for the tips, I tried with changing the mutableArray by adding and removing items, it seems like the array (not mutable) will reflecting to the updated mutable array without issue too.
Yeah, but that's not what I meant... for example you might keep the count of that array somewhere else. and when you access the object with that countNumber and the array changed... it's not what it's intended. but with a copy, the array will never change.
I get your point. As the downside is we can not store the information like count as another variable but always need to use "array.count" instead. Thanks!
0

Your array will stay mutable, but compiler will think that it is immutable. So it won't allow you to use methods specific for NSMutableArray without typecasting.

2 Comments

Yea, I am aware of that. So it only lost the methods of mutable class and will not cause other issue?
It won't lost methods. You can send messages without warning in this way. [myArray performSelector:@selector(removeAllObjects)]; . Assigning NSMutableArray to NSArray will not change object.

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.