0
votes

My application always popping 'mutating method sent to immutable object' My dictionary and the array already declare to be a mutable one but seems it doesn't help I have tried the replaceObjectAtIndex function it also didn't work.

NSString* plistpath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
NSMutableDictionary* dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:plistpath];
NSMutableArray* Array1 = [dictionary objectForKey:@"Array1"];
[Array1 removeObjectAtIndex:num];
[Array1 insertObject:@"1" atIndex:num];
return Array1;

thanks

2
How did did you add Array1 to dictionary and how did you create it before adding it? - sch

2 Answers

1
votes

Try:

NSArray *array = [dictionary objectForKey:@"Array1"]; // Immutable
NSMutableArray * Array1 = [NSMutableArray arrayWithArray:array]; // Mutable
// ...

Or as Julien noted:

NSArray *array = [dictionary objectForKey:@"Array1"]; // Immutable
NSMutableArray * Array1 = [array mutableCopy]; // Mutable
0
votes

If the right hand side does not return an NSMutableArray (NSArray instead) you try to mutate an in mutable object.

NSMutableArray* Array1 = [dictionary objectForKey:@"Array1"]; // NSArray