mutating method sent to immutable object - error
ios, objective-c
Solution
The problem with assigning to a NSMutableArray is, it will only work if defaultDefects was assigned an NSMutableArray for the given key.
Note: NSUserDefaults always returns an immutable object.
Do this instead
NSMutableArray *loadDefects = [[defaultDefects objectForKey:@"defaultDefects"]mutableCopy];
this guarantees a mutable copy.
Problem
``` NSUserDefaults *defaultDefects = [NSUserDefaults standardUserDefaults]; NSLog(@"%f %f", self.defectPositionX, self.defectPositionY); NSMutableArray *loadDefects = [defaultDefects objectForKey:@"defaultDefects"]; NSLog(@"%f %f", self.defectPositionX, self.defectPositionY); if (loadDefects == nil) { loadDefects = [NSMutableArray array]; } NSLog(@"%f %f", self.defectPositionX, self.defectPositionY); //PROBLEM HERE [loadDefects addObject:[NSNumber numberWithDouble:self.defectPositionX ]]; [loadDefects addObject:[NSNumber numberWithDouble:self.defectPositionY ]]; NSLog(@"%f %f", self.defectPositionX, self.defectPositionY); [defaultDefects setObject:loadDefects forKey:@"defaultDefects"]; NSLog(@"%f %f", self.defectPositionX, self.defectPositionY); [defaultDefects synchronize]; NSLog(@"%f %f", self.defectPositionX, self.defectPositionY); ViewControllerImage *secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerImage"]; secondViewController.thicknessValue1 = self.thicknessValue1; secondViewController.capWidthValue1 = self.capWidthValue1; NSLog(@"%f %f", self.defectPositionX, self.defectPositionY); [self presentViewController:secondViewController animated:YES completion:nil]; NSLog(@"%f %f", self.defectPositionX, self.defectPositionY); ``` Logs this ``` 2014-04-21 17:03:02.838 U[8958:60b] 169.728546 274.674475 2014-04-21 17:03:02.840 U[8958:60b] 169.728546 274.674475 2014-04-21 17:03:02.842 U[8958:60b] 169.728546 274.674475 ``` The full error report is this * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object' * First throw call stack: (0x2e8e2f03 0x39077ce7 0x2e8e2e45 0x2e85642b 0xb93d3 0x311476c7 0x31147663 0x31147633 0x31132d7b 0x3114fa3d 0x31146c7d 0x31141ca7 0x31116e75 0x31115541 0x2e8adfe7 0x2e8ad4af 0x2e8abc9f 0x2e8167a9 0x2e81658b 0x337836d3 0x31175891 0xb7851 0x39575ab7) libc++abi.dylib: terminating with uncaught exception of type NSException I am confused at how I have it as a Mutable Array yet it's calling it Immutable meaning I cannot mutate it.. It's the first time I've come across this error and I am struggling to grasp it. When running the app and I save the data into the mutable array, sometimes it works and then sometimes it crashes...?