我用一个按钮创建一个简单合适的视图。 按下按钮时,新视图消耗和一个用户联系人列表显示。 在此视图中,放置了两个字段用户名和用户地址。
现在我的问题是当我按下填写我的文本字段的保存按钮时,内容保存在字典表格的数组中,并在我的表格视图中显示人物的姓名。
但是当我再次填写新用户的数据并保存时,我的新词典将覆盖在旧词典上。
请帮助我,我想为每个人要求每本字典。
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];