I have this code;
for (int i = 0; i<period+1; i++){
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];
NSDate *newData = [dataToAdd dateByAddingTimeInterval:60*60*24*j];
j++;
NSString *data = [dateFormatter stringFromDate:newData];
[[appDelegate.globalArray objectAtIndex:[name intValue]]addObject:data];
[dateFormatter release];
}
this code work fine until I go in background.
appDelegate.globalArray is a global array and when I go in background I store it with NSUserDefault
- (void)applicationDidBecomeActive:(UIApplication *)application {
globalArray = [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"globalArray"]];}
and
- (void)applicationDidEnterBackground:(UIApplication *)application{
[[NSUserDefaults standardUserDefaults] setObject:globalArray forKey:@"globalArray"];}
the loop work fine until I go in background but when I reopen the application and I enter in this loop I have an exception
“NSCFArray insertObject:atIndex:]: mutating method sent to immutable object”
why?
Is
globalArrayan array of arrays? If so, then you need to ensure that each array inglobalArrayis an instance ofNSMutableArrayas well. I believe (correct me if I’m wrong) that when you get an array back fromNSUserDefaults, you always get an immutable array. The same goes for your nested arrays.In that case, you could try the following:
There may be a more efficient way to do this, but you get my point…