This is my first question on Stack Overflow, and I hope it's pretty simple. I have the following code that I wrote to figure out exactly what I'm adding to 'outerArray' in this case:
NSMutableArray *contents = [[NSMutableArray alloc] initWithContentsOfFile:path];
outerArray = [[NSMutableArray alloc] initWithCapacity:2];
[outerArray addObject:contents];
[contents removeObjectAtIndex:0];
[contents release];
contents = nil;
NSMutableArray *test = [outerArray objectAtIndex:0];
NSLog(@"Test at 0: %@", [test objectAtIndex:0]);
Now, the confusing part is this: What I see is the element originally at index 1 of 'contents' being written out by NSLog. This doesn't make sense to me:
The only thing I can think is that I am adding a pointer to 'contents' to 'outerArray', but that releasing it doesn't overwrite the data. So, I'm basically lucky at this point that the NSLog isn't failing, but it could, later.
Is that correct?
What you see is correct: NSMutableArray retains objects added to it, so contents remains live even though your code has released its reference to it. The second reference (from inside outerArray) is what holds your inner array formerly known as contents from being released. There is no copying involved.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With