每次保存新数据时,Plist都会重写旧数据

时间:2015-12-08 06:38:42

标签: ios objective-c plist

我有一个简单的目标c程序,我在其中将用户输入的数据保存在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

1 个答案:

答案 0 :(得分: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。