0

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

  }

}

4

1 に答える 1

1

U はsave NSArrayこのようDocument Directoryにできます。

:savingretrieving filenameままsameです。

それもeasy retrieve saved arrayからdoc dirです。

編集:使用する方datacomplex and large良い場合SQLITE


編集:ボタンview Controller'sに追加でfavouriteクリックevent

 //check favourite saved in doc dir
 if(!saved) //not saved
   //NSMutableArray has not data first time
   //[NSMutableArray addObject:newFavourite];
   //save new data in doc dir
 else // saved
   //firstly retrieve data from doc dir as NSMutableArray
   //[NSMutableArray addObject:newFavourite];
   //Now save again NSMutableArray in doc dir.

followingこれらのinstructionファイルによってpermanentlyDocuments folder.

于 2013-01-07T08:53:26.040 に答える