我创建了一种将数据写入plist的方法。
- (void)writeToPListUserType:(NSString *)type
andUserID:(NSString *)usid
andFirstName:(NSString *)fname
andLastName:(NSString *)lname
andPassword:(NSString *)pwd{
NSMutableDictionary *userDetails = [[NSMutableDictionary alloc] init];
[userDetails setObject:fname forKey:@"firstName"];
[userDetails setObject:lname forKey:@"lastName"];
[userDetails setObject:pwd forKey:@"password"];
NSMutableDictionary *userIDKey = [[NSMutableDictionary alloc] init];
[userIDKey setObject:userDetails forKey:usid];
[userCredentials setObject:userIDKey forKey:type];
[userIDKey release];
[userDetails release];
NSString *error;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:
@"userCredentials.plist"];
NSData *plistData = [NSPropertyListSerialization
dataFromPropertyList:userCredentials
format:NSPropertyListXMLFormat_v1_0
errorDescription:&error];
[plistData writeToFile:plistPath atomically:YES];
}
我能够将新数据写入plist。但是,在写入新数据时,旧数据不再存在。换句话说,该方法是覆盖我的plist 请告诉我这个方法有什么问题...... :(
提前致谢...
答案 0 :(得分:1)
1)从磁盘读取plist
2)在内存副本中创建一个可变的
3)修改(在内存中)plist
4)使用内存plist
覆盖您读取的文件答案 1 :(得分:1)
这一行:
code>[userCredentials setObject:userIDKey forKey:type];
你正在用新数据覆盖现有的“userCredentials”“type”键(我看不到它在方法中新分配)。
因此,我认为userCredentials是一个在其他地方创建的可变字典,会被新数据覆盖。