覆盖而不是写入 plist-cocoa
我创建了一种将数据写入 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。
请告诉我该方法有什么问题...:(
提前感谢...
I have created a method to write data to a 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];
}
I am able to write new data to the plist. However, when the new data is written, the old data is no longer there. In other words, the method is overwriting my plist.
Please tell me what is wrong with the method... :(
Thanks in advance...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有了这一行:
您正在用新数据覆盖现有的“userCredentials”“type”键(我没有看到它是在方法中新分配的)。
因此,我认为 userCredentials 是在其他地方创建的可变字典,它被新数据覆盖。
With this line:
you are overwriting the existing "userCredentials" "type" key (I don't see it is newly allocated in the method) with new data.
So the userCredentials, which I suppose is a mutable dictionary created elsewhere, is overwritten with the new data.
1) 从磁盘读取 plist
2) 在内存中创建可变副本
3) 修改(内存中)plist
4) 使用内存中 plist 覆盖您读取的文件
1) read the plist from disk
2) create a mutable in memory copy
3) modify the (in memory) plist
4) overwrite the file you read, using the in memory plist