Hà Bùi
Hà Bùi

Reputation: 33

Strange result when inserting object to NSMutableArray at Index 1

In my application, I want to insert object into NSMutableArray:

NSMutableArray *test = [NSMutableArray arrayWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:3], [NSNumber numberWithInt:4], nil];
[test insertObject:[NSNumber numberWithInt:2] atIndex:1];

I put breakpoint at second code line and the result in debug area is OK:

self = (MasterViewController *) 0x074b9cb0
test = (NSMutableArray *)   0x07462c50 @"3 objects"
   [0] = (id)   0x07461f10 (int) 1
   [1] = (id)   0x07473ec0 (int) 3
   [2] = (id)   0x074cbd30 (int) 4

But after run second code line, the result is unexpected:

self = (MasterViewController *) 0x074b9cb0
test = (NSMutableArray *)   0x07462c50 @"4 objects"
   [0] = (id)   0x074713f0 (int) 2
   [1] = (id)   0x07473ec0 (int) 3
   [2] = (id)   0x074cbd30 (int) 4
   [3] = (id)   0x07461f10 (int) 1

Can anyone explain why after insert at index 1 the first object (int 1) become last object, or this case is bug of ios 6.1?

When insert object at index > 1, there is no problem.

Upvotes: 2

Views: 126

Answers (1)

borrrden
borrrden

Reputation: 33421

I rarely trust the results that come out of that area. Use debugger commands instead. The array is getting created correctly. I copied and pasted those two lines and I got

(lldb) po test
$0 = 0x1dda1ad0 <__NSArrayM 0x1dda1ad0>(
1,
3,
4
)

After the first line, and

(lldb) po test
$1 = 0x1dda1ad0 <__NSArrayM 0x1dda1ad0>(
1,
2,
3,
4
)

after the second line. However, the results are the way you say in the variables window. The moral of the story is, trust the debugger instead of the variables window!

Upvotes: 3

Related Questions