I have a table view set up and need to save it when the app quits and reload when it reopens. I have decided instead going through the delegate, to save the mutable array every time it is changed. I have the following method to save the array
Code:
-(void)save{
NSUserDefaults *arraydefaults = [NSUserDefaults standardUserDefaults];
[arraydefaults setObject:items forKey:@"items"]; //items is the mutable array i need to save
[arraydefaults synchronize];
}
To reload the array, i have the following code
Code:
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"items"] == nil){
items = [[NSMutableArray alloc] init];
}
else{
NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
items = [[NSMutableArray arrayWithArray:[settings objectForKey:@"items"]]mutableCopy];
}
When I restart the app then try to add a new item to the array it crashes. If I comment out the if statement, It behaves as normal but doesn't save.
Anyone help greatly appreciated