我有几个文件要保存在 Documents 文件夹中,这个文件列表放在一个可变数组中。文件必须保留在 iPhone 应用程序的内存中也关闭。第一个文件已保存,但是当我更改视图控制器然后返回时,可变数组变为空。如何将数据保存在 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
}
}