如何在plist文件上的数组上设置多个字典?

时间:2016-11-27 15:27:41

标签: ios

我用一个按钮创建一个简单合适的视图。 按下按钮时,新视图消耗和一个用户联系人列表显示。 在此视图中,放置了两个字段用户名和用户地址。

现在我的问题是当我按下填写我的文本字段的保存按钮时,内容保存在字典表格的数组中,并在我的表格视图中显示人物的姓名。

但是当我再次填写新用户的数据并保存时,我的新词典将覆盖在旧词典上。

请帮助我,我想为每个人要求每本字典。

代码是

NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"sample.plist"];

NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath:plistPath]) {
    NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"plist"];
    [fileManager copyItemAtPath:sourcePath toPath:plistPath error:nil];
}

NSLog(@"%@",plistPath);

NSString *personName = self.nameField.text;
NSString *personAddress = self.addressField.text;
NSString *nameKay = @"name";
NSString *AddressKey = @"address";

NSArray *values = [[NSArray alloc] initWithObjects:personName,personAddress, nil];
NSArray *keys = [[NSArray alloc] initWithObjects: nameKay, AddressKey, nil];

NSDictionary *dict = [[NSDictionary alloc] initWithObjects:values forKeys:keys];


[self.plistContainer addObjectsFromArray:dict];
NSLog(@"%@", self.plistContainer);

[self.plistContainer writeToFile:plistPath atomically:YES];

[self dismissViewControllerAnimated: YES completion: nil];

图像是

enter image description here

App Image

enter image description here

0 个答案:

没有答案