2013-01-07 2 views
0

문서 폴더에 저장할 파일이 여러 개 있습니다.이 파일 목록은 변경 가능한 배열에 저장됩니다. 파일은 닫힌 iPhone 응용 프로그램의 메모리에 남아 있어야합니다. 첫 번째 파일은 저장되지만 뷰 컨트롤러를 변경 한 다음 다시 돌아 가면 변경 가능한 배열이 비게됩니다. 어떻게 데이터를 Documents 폴더에 메모리에 보관합니까? DetailExpViewController.mDocuments 폴더에 파일 목록을 작성하고 메모리에 보관하십시오.

에서 DetailExpViewController.h

//.... 
    @property (nonatomic, strong)NSMutableArray *arrayFavorites; 
    @property (nonatomic, assign)BOOL existsArray; 
    //.... 
    @end 

에서

- (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 

    } 

}

+0

항상 nil을하고 다음 U는 그것을 파일 –

+0

을 ALLOC. 내 응용 프로그램에는 사용자가 단추를 누르면 "즐겨 찾기 추가"라는 단추가있는보기 컨트롤러가 있으며 파일은 문서 폴더에 저장해야합니다. 그렇지 않으면 그렇지 않습니다. 나는 사용자를 저장할 것이 무엇인지를 미리 알 수 없으므로 가변 배열을 만들었습니다. 이러한 파일은 문서 폴더에 영구적으로 남아 있어야합니다. 어떻게해야합니까? –

+0

데이터를 저장하기위한 SQLite는 사용하지 말아 이유 –

답변

1

U 수 save NSArraythis 같은 Document Directory한다.

: savingretrievingfilenamesame 남아있다.

또한 easy retrievesaved 입니다.

편집 : datacomplex and large를보다 효율적으로 사용 SQLITE 경우.


편집는 : view Controller's에서 event을 클릭 favourite에 추가 버튼 :

//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 파일이 Documents folder에서 permanently 남아있다. 보기 didload 배열의

+0

의 목록이 나는 같이 변경 가능한 배열 정적 배열이 아닌이 있기 때문에 항상 –

+0

참고 링크 예제는 정적 데이터를 보여줍니다. 하지만 다이나믹 데이터를 가질 수 있습니다. –

+0

대단히 감사합니다. –

관련 문제