Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mutating method sent to immutable object

When I use this method first time it works fine, but when I called it second time I get the error "mutating method sent to immutable object". The problem is at line with "addObject" command.

-(IBAction) save: (id) sender{

NSMutableArray *placesT= [[NSUserDefaults standardUserDefaults] objectForKey:@"placesT"];

if (!placesT) {
    placesT=[[[NSMutableArray alloc] init] autorelease];
}

[placesT addObject: [NSString stringWithFormat:@"%@", tagF.text] ];

NSUserDefaults *tUD=[NSUserDefaults standardUserDefaults];
[tUD setObject:placesT forKey:@"placesT"];
[tUD synchronize];

[self dismissModalViewControllerAnimated:YES];

}

like image 510
gbaor Avatar asked Apr 26 '11 12:04

gbaor


1 Answers

As the documentation for NSUserDefaults says: "Values returned from NSUserDefaults are immutable, even if you set a mutable object as the value." Whenever you want to change a collection you get from NSUserDefaults you have to get the immutable version, make a mutableCopy, modify that, and set it back again.

like image 170
smorgan Avatar answered Oct 21 '22 04:10

smorgan