一、第一种是在没有plist文件的情况下
NSDictionary *dict1 = [NSDictionary dictionaryWithObject:@"john1" forKey:@"name1"];
NSString *path1 = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/user1.plist"];
<span style="color:#FF0000;">// 注意这里不能使用 NSMutableDictionary *dictPlist1 = [[NSMutableDictionary alloc] initWithContentsOfFile:path1];
// 因为plist文件还不存在,所以初始化后dictPlist1会是空的</span>
NSMutableDictionary *dictPlist1 = [NSMutableDictionary dictionary];
[dictPlist1 setValuesForKeysWithDictionary:dict1];
[dictPlist1 writeToFile:path1 atomically:YES];
二、第二种是在有plist文件的情况下,用于中途插入数据
NSDictionary *dict2 = [NSDictionary dictionaryWithObject:@"john2" forKey:@"name2"];
NSString *path2 = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/user2.plist"];
<span style="color:#FF0000;">// 这里使用不能使用上面那种初始化,那样会把之前的数据初始化</span>
NSMutableDictionary *dictPlist2 = [[NSMutableDictionary alloc] initWithContentsOfFile:path2];
[dictPlist2 setValuesForKeysWithDictionary:dict2];
[dictPlist2 writeToFile:path2 atomically:YES];