覆盖而不是写入 plist-cocoa

发布于 2024-10-15 08:05:51 字数 1535 浏览 2 评论 0原文

我创建了一种将数据写入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝不服输 2024-10-22 08:05:51

有了这一行:

code>[userCredentials setObject:userIDKey forKey:type];

您正在用新数据覆盖现有的“userCredentials”“type”键(我没有看到它是在方法中新分配的)。

因此,我认为 userCredentials 是在其他地方创建的可变字典,它被新数据覆盖。

With this line:

code>[userCredentials setObject:userIDKey forKey:type];

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.

筑梦 2024-10-22 08:05:51

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文