2015-12-08 96 views
0

我有一個簡單的客觀c程序,其中我將用戶輸入的數據保存在plist中。但是,當我嘗試在plist中保存多個數據時,它會重寫舊數據,並且我只能在我的plist中獲取一個數據,即用戶輸入的最後一個數據。這是我下面的代碼保存數據 -Plist每次重寫舊數據我保存一個新數據

- (IBAction)savedata:(id)sender 
{ 

    NSError *error; 
    NSString *arr= [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 

    arr=[arr stringByAppendingPathComponent:@"datalist.plist"]; 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSMutableArray *valuearray = [[NSMutableArray alloc]init]; 
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init]; 
    [dict setValue:_nametextfield.text forKey:@"NAME"]; 
    [valuearray addObject:dict]; 

    if ([fileManager fileExistsAtPath:arr]) 
    { 
     NSLog(@"FILE EXIST"); 
     self.bundle = [[NSBundle mainBundle]pathForResource:@"datalist" ofType:@"plist" ];//NSString *bundle = [[NSBundle mainBundle] pathForResource:@」data」 ofType:@」plist」]; 
     NSLog(@"%@",_bundle); 

     [fileManager copyItemAtPath:_bundle toPath: arr error:&error]; 

     [valuearray writeToFile:arr atomically:YES]; 
     NSLog(@"%@",arr); 

     //6 
    } 
    else 
    { 
     NSLog(@"FILE DOESN'T EXIST"); 
     [valuearray writeToFile:arr atomically:YES]; 
    } 


} 

這裏是我取的數據 -

- (IBAction)fetchdata:(id)sender 
{ 
    NSString *arr= [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 

    arr=[arr stringByAppendingPathComponent:@"datalist.plist"]; 
    if ([[NSFileManager defaultManager]fileExistsAtPath:arr]) 
    { 
     NSArray *dict=[NSArray arrayWithContentsOfFile:arr]; 
     NSLog(@"%@",dict); 

    } 

} 

現在我只能檢索最後輸入的數據,我不知道爲什麼會造成這麼多問題。請有人幫助我。我還附上了我的主包的截圖。 enter image description here

+2

對'NSArray writeToFile:atomically:'的調用會替換文件。每次寫入文件時,都需要在文件中寫入所有需要的數據。 – rmaddy

+0

@rmaddy那麼該怎麼做? –

+1

您編寫的數組需要包含所有數據,而不僅僅是新數據。 – rmaddy

回答

1

更換保存部分與此 -

- (IBAction)savedata:(id)sender 
{ 
    NSError *error; 
    NSString *arr= [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 

    arr=[arr stringByAppendingPathComponent:@"datalist.plist"]; 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSMutableArray *valuearray = [[NSMutableArray alloc]init]; 
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init]; 
    [dict setValue:_titletextfield.text forKey:@"NAME"]; 
    [valuearray addObject:dict]; 

    if ([fileManager fileExistsAtPath:arr]) 
    { 
     NSLog(@"FILE EXIST"); 
     //self.bundle = [[NSBundle mainBundle]pathForResource:@"datalist" ofType:@"plist" ];//NSString *bundle = [[NSBundle mainBundle] pathForResource:@」data」 ofType:@」plist」]; 
     // NSLog(@"%@",_bundle); 

     //[fileManager copyItemAtPath:_bundle toPath: arr error:&error]; 
     NSMutableArray *valuearray = [[NSMutableArray alloc]initWithContentsOfFile:arr]; 
     if (dict.count) 
     { 
      [valuearray addObject:dict]; 
     } 
     else 
     { 
      NSLog(@"data not retrieved"); 
     } 

     [valuearray writeToFile:arr atomically:YES]; 
     NSLog(@"%@",arr); 

     //6 
    } 
    else 
    { 
     NSLog(@"FILE DOESN'T EXIST"); 
     [valuearray writeToFile:arr atomically:YES]; 
    } 

    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"sucess" message:@"successfully saved" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil]; 
    [alert show]; 
    [self.navigationController popToRootViewControllerAnimated:YES]; 
} 

感謝@rmaddy。