i have looked all over the place regarding this problem. All i am trying to do is, create a nsuser defaults object, then add a mutable array to it and try to modify the nsuserdefaults object.
//Created a sample array
NSMutableArray *xml=[[NSMutableArray alloc] init];
[xml addObject:@"x"];
[xml addObject:@"x"];
[xml addObject:@"x"];
[xml addObject:@"x"];
[xml addObject:@"x"];
//assigned to defaults object which is created previously with a mutableCopy
[defaults setObject:[xml mutableCopy] forKey:@"userLocationsDetailedXML"];
//Tried to modify the defaults object - which at this point should be mutable
[[defaults objectForKey:@"userLocationsDetailedXML" ] replaceObjectAtIndex:1 withObject:@"y"] ;
Adding [defaults synchronize] does’nt help either.
But surprisingly the defaults object is still immutable. It is __NSCFArray, not __NSCFArrayM as expected
Any suggestions?
edit: console output
Class : __NSCFArray => SHould’nt it be _NSCFArrayM since i created a mutable copy?
[_NSCFArray replaceObjectAtIndex:withObject:]: mutating method sent to immutable object’
Try casting the array as NSMutableArray, using
that should take care of it.
EDIT:
Given that you can’t actually store NSMutableArrays in
NSUserDefaults, I recommend that you just go ahead and reassign the entire thing, like so:Then, for changing the values,