I have several files to be saved in the Documents folder, this list of files put in a mutable array. The files must remain in the memory of the iPhone application also closed. The first file is saved, but when I change view controllers and then go back the mutable array becomes empty. How do I keep the data in memory in the Documents folder?
in DetailExpViewController.h
//....
@property (nonatomic, strong)NSMutableArray *arrayFavorites;
@property (nonatomic, assign)BOOL existsArray;
//....
@end
in DetailExpViewController.m
- (void)viewDidLoad
{
//.....
if (!self.existsArray){
/*when I load the view controller this array is always nil even
has already been created*/
self.arrayFavorites =[[NSMutableArray alloc]init];
}else{
NSLog(@"The array exists");
}
//....
}
-(void) addFavorites:(id)sender{
//.....
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString * text= [NSString stringWithFormat:@"%@.txt",self.name];
NSString * documentsPath = [documentsDirectory stringByAppendingPathComponent: text];
[self.arrayFavorites addObject:text];
[self.arrayFavorites writeToFile:documentsPath atomically:YES];
self.existsArray=YES;
for (id obj in self.arrayFavorites) {
NSLog(@"%@", self.arrayFavorites);
//only writes the file at the time, then it is deleted
}
}
U can
save NSArrayinDocument Directorylike this.Note :
savingandretrievingfilenameremainssame.Its also
easy retrievesavedarrayfromdoc dir.EDIT : If
dataiscomplex and largebetter useSQLITE.EDIT : In
view Controller'sAdd tofavouritebutton clickevent:By
followingtheseinstructionfiles remainpermanentlyin theDocuments folder.