0

All,

I have strings stored in a NSMutableArray. When a certain condition is met elsewhere in the code, the object that is FIRST in the NSMutableArray (read, objectAtIndex:0) gets removed. After it is removed, I need to shift everything down an index. Here is my example:

/*
    Index/Value
    0 - one
    1 - two
    2 - three
    3 - four
    4 - five
    5 - six
    6 - seven
*/
//removes object...
[mArray removeObjectAtIndex:0];
//I am missing something here...


//I want the value of the array to be as follows:
/*
    Index/Value
    0 - two
    1 - three
    2 - four
    3 - five
    4 - six
    5 - seven
*/

What am I missing?

EDIT: The NSMutableArray is stored in NSUserDefaults. Is that what may be causing this not to work?

EDIT2: Because it is stored in NSUserDefaults, it changes to immutable. Check out the accepted answer for the correct way to do it. Details here: crashes my app [NSMutableArray1 removeAllObjects] iphone sdk

3 Answers 3

7

You're not missing anything. The indexes will be exactly like that after removing the object at index 0.

Sign up to request clarification or add additional context in comments.

10 Comments

I thought this was the case, but it throws an error "*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object'" Maybe I have something wrong somewhere else in my code.
That is because you do not have a mutable array. mArray needs to be an NSMutableArray either by creating a new one or calling mutableCopy (which will return a retained copy) on an existing array.
@James That error means that your array is actually a normal NSArray, not an NSMutableArray. Check the place you initialize your array to make sure it's really being created as an NSMutableArray.
NSUserDefaults always returns an immutable array. Although you are setting it to mutable it will return immutable so when get the value back also call mutableCopy.
The moral of this story is always post all relevant code, especially variable initialization. If Joe is correct, his comment should be added as an answer.
|
6

If I understand your issue correctly you are storing an NSMutableArray to NSUserDefaults and then when you load it out of NSUserDefaults it throws an error when you try to mutate?

This is because when you retrieve the array from NSUserDefaults it will be an instance NSArray not NSMutableArray therefore you need to make a an instance of NSMutableArray to work with.

NSMutableArray *mArray = [[NSMutableArray alloc] initWithArray:myArrayFromNSUserDefaults];

1 Comment

This worked perfectly. I also found an answer here as well for those who may stop by later: stackoverflow.com/questions/3955969/…
0

That should work. Are you doing something like this?

    NSMutableArray *mArray = [[NSMutableArray alloc]init];
    [mArray addObject:@"one"];
    [mArray addObject:@"two"];
    [mArray addObject:@"three"];

    [mArray removeObjectAtIndex:0];

    NSLog(@"mArray at 0: %@",[mArray objectAtIndex:0]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.