0

i got this exception:

[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance

when i try to replace a specific element by another:

EDIT: this is my whole code:

//declaring an AppDelegate instance
    AppDelegate *myAppDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];

    //get the array in which we have stored all the choosed themes
    NSMutableArray *aMutableArray=myAppDelegate.themesChoosed;


    for (int i=0; i<[aMutableArray count]; i++) {
            NSString *str=[NSString stringWithString:[aMutableArray objectAtIndex:i]];
            if ([str isEqualToString:@"B1"]) {
                [aMutableArray replaceObjectAtIndex:i withObject:@"B2"];
          }
    }

I maked sure that the B1 element does exist in the array.

3 Answers 3

1

What is happening to your NSMuatbleArray before you get into the for loop?

Is it a property? If so, what is the property declaration? Did you use copy?

If you implement a property like this:

@property (nonatomic, copy) NSMutableArray *myArray;

...then you can run into problems like this because the synthesized setter sends copy to the array, which results in an immutable copy. If this is the case, you need to implement your own setter that calls mutableCopy on the array (or just use retain instead and design your code a little differently).

EDIT:

Based on your comments below and the updated code, I'm sure the problem must be something to do with the array on the app delegate not being mutable.

Try this:

NSMutableArray *mutableThemeseChoosed = [NSMutableArray arrayWithArray:myAppDelegate.themesChoosed];
Sign up to request clarification or add additional context in comments.

7 Comments

I didn't implement a property for that array, i just define it when i need: NSMutableArray *aMutableArray=anotherMutableArray;, should i property it? i am using the ARC.
There's no reason that you should need a property for this to work. Have you tried the above with a new muableArray that isn't from 'anotherMutableArray'?
Perhaps if you updated the question with more of your code, we might be able to spot something wrong?
Unfortunately, that was all my code, it's independent of the other code. my NSMutableArray contain the word B1 which i want to replace it y the word B2
@Malek well, your application does not agree with this, so maybe you could show us how do you create this array, and how is it assigned to appDelegate.themesChoosed?
|
0

I just tried your code and it works fine.

NSMutableArray *aMutableArray = [NSMutableArray arrayWithObjects:[NSString stringWithString:@"A1"],[NSString stringWithString:@"B1"],[NSString stringWithString:@"B2"],[NSString stringWithString:@"A1"],[NSString stringWithString:@"A2"],[NSString stringWithString:@"A1"],[NSString stringWithString:@"A1"], nil];

NSLog(@"%@",aMutableArray);

for (int i=0; i<[aMutableArray count]; i++) {
    NSString *str=[NSString stringWithString:[aMutableArray objectAtIndex:i]];
    if ([str isEqualToString:@"B1"]) {
        [aMutableArray replaceObjectAtIndex:i withObject:@"B2"];
    }
}

NSLog(@"%@",aMutableArray);

Comments

0

Your problem is - as the error tells - that your mutable array is a NSArray (which is not mutable)

What you get out of myAppDelegate.themesChoosed; is likely a NSArray. Try the following: NSMutableArray *aMutableArray= [NSMutableArray arrayWithArray:myAppDelegate.themesChoosed];

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.