Documents フォルダーに保存するファイルがいくつかあります。このファイルのリストは変更可能な配列に配置されます。ファイルは、iPhone アプリケーションも閉じたメモリに残す必要があります。最初のファイルは保存されますが、View Controller を変更してから戻ると、可変配列が空になります。Documents フォルダのメモリにデータを保持するにはどうすればよいですか?
DetailExpViewController.h で
//....
@property (nonatomic, strong)NSMutableArray *arrayFavorites;
@property (nonatomic, assign)BOOL existsArray;
//....
@end
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
}
}