user2083364
user2083364

Reputation: 764

A bug with NSMutableArray of NSString?

NSString *str = @"1 2 3 4 5";
NSMutableArray *strArray = [[[str componentsSeparatedByString:@" "] mutableCopy] autorelease];
[strArray removeObjectAtIndex:3];
[strArray removeObjectAtIndex:0];

At the end of this code I expect that array contains @"2", @"3", @"5".

But it contains 0x00000, @"2", @"3".

How to fix it? Numbers are for example only, there could be various strings of various length separated with spaces.

UPDATED

It is strange, but it really writes 2, 3, 5 into console.

But here is the same array in debug window:

enter image description here

Upvotes: 0

Views: 126

Answers (4)

JeremyP
JeremyP

Reputation: 86661

Here is what I think is happening. What you have is, if anything, a bug in the debugger.

Let's assume for the moment that NSMutableArray is represented by a normal C array internally. The naive implementation of -removeObjectAtIndex: would shuffle up all the objects after the one you want to remove into the previous slot in the array. This has O(n) complexity, where n is the number of objects in the array.

An optimisation might be to associate a "base index" with the array which will be subtracted from the index supplied to any method that takes an index as a parameter. That way, [foo removeObjectAtIndex: 0] can be implemented by simply assigning nil to the first element and bumping the base index for constant time complexity.

I'm sure the real implementation is more complex than that, but the above just serves to illustrate the idea.

If the debugger doesn't know about this, it would display the underlying C array exactly as it does in the screen cap your question. The point is that you can't really trust the debugger on anything where it can't be expected to know the internals of the object. Printing the description to the console is a much more reliable method of examining the object - as long as -description has a helpful implementation.

Upvotes: 1

Alex
Alex

Reputation: 2160

remove your autorelease! Fine working for me!

NSString *str = @"1 2 3 4 5";
    NSMutableArray *strArray = [[str componentsSeparatedByString:@" "] mutableCopy];
    [strArray removeObjectAtIndex:3];
    [strArray removeObjectAtIndex:0];
    NSLog(@"strArray = %@", strArray);

2013-10-16 14:29:35.537 Notes[566:c07] strArray = ( 2, 3, 5 )

Upvotes: 0

Hussain Shabbir
Hussain Shabbir

Reputation: 15025

Not sure why your code is not working its working fine for me, also tried the same in other way below :-

NSString *str = @"1 2 3 4 5";
NSArray *arr1= [str componentsSeparatedByString:@" "];
NSMutableArray *strArray=[NSMutableArray arrayWithArray:arr1];
[strArray removeObjectAtIndex:3];
[strArray removeObjectAtIndex:0];
NSLog(@"%@",strArray);

Output:-

2
3
5

Upvotes: 2

Alex
Alex

Reputation: 1051

Why aren't you using ARC? I have tested that code using ARC and it works. So, can you explain it a little bit more what you are trying to do? (I could't added it as comment)

Upvotes: 0

Related Questions