I am trying to save an NSMutableArray on device , but everytime I reload the application the data is erased.
I tried to use NSUserDefaults but the problem persists
here is the code I am using
-(void) addingData{
[data addObject:theBarcodeString]; //data is the NSMutableArray
[mainTableView reloadData]; //this is a UITableView where the data from the NSMutableArray is populated
[self endText];
history=[NSUserDefaults standardUserDefaults];
[history setObject:data forKey:@"history" ];
}
and here is where I load the data
- (void)viewDidLoad
{
...
if (data == nil) {
data = [[NSMutableArray alloc] init]; //onload it always goes to his line
else {
data=[[NSUserDefaults standardUserDefaults] objectForKey:@"history" ];
}
...
}
what am I doing wrong ?
You shouldn’t be testing to see if data == nil. Instead, just do:
Note that you will have to release that later.
If you want
datato be a mutable array, you must use:If you try to performing mutating operations on data without using mutableArrayValueForKey, you will get a crash, because all objects returned from NSUserDefaults are immutable.