[items addOject:@"One"] not inserted at index 0?


#1

Hi,

Just wondering on page 47 after we alloc and init NSMutableArray *items how come the first call to addObject the Object @“One” wasn’t inserted at Index 0 but instead inserted at index 1?

NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:@“One”];
[items addObject:@“Two”];
[items addObject:@“Three”];
[items insertObject:@“Zero” atIndex:0];


#2

I hope I correctly understand your question. Does the following explain what you are wondering about?

[items addObject:@“One”]; -> the “One” object now has index 0

[items insertObject:@“Zero” atIndex:0]; -> The “Zero” object is inserted at index 0, so the “One” object now has index 1


#3

Thanks soulianis,

Yes that answers my question, @“One” has been shifted from index 0 to index 1 after [items insertObject:@“Zero” atIndex:0].

I realised it after I posted the question.