0

How is NSMutableArray implemented?

If there are a number of NSMutableArray references to a mutable array e.g. Source Array and each of these pointing arrays sort the objects differently, are the objects copied over into each array or are the object pointers just internally rearranged?

If the source array is updated with new objects, will the NSMutableArray pointers automatically add an the entry to their internal reference.

2 Answers 2

1

It just stores pointers.This means that if the NSMutableArray stores immutable objects, then you are sure that they'll not change their state, but if it has a mutable object the state of any referenced object may change at any time.
Exactly I don't know how it's implemented, but I would say a pointer to id.It allocates memory and then update the id at the index that needs:

id* pointers;
< Allocation>
pointers[i]= newObjectPointer;
Sign up to request clarification or add additional context in comments.

Comments

1

It's a little hard to parse exactly the scenario you're asking about, but NS(Mutable)Array holds a list of object references. It does not copy the objects you add to it, just keeps a pointer. If those objects can change, and you change them from outside the array, that will be reflected in the things you read back from the array.

NSMutableArray * arr = [NSMutableArray array];
NSMutableArray * child = [NSMutableArray array];
[child addObject:@5];
[arr addObject:child];
[child addObject:@6];
NSLog(@"%@", arr[0]);
> [5, 6] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.