3

通过编码检查数据是否已经存在后,如何覆盖 plist 中的现有数据

详细地:

我将用户名和密码保存在位于文档目录中的 plist 文件中。现在,如果用户选择更改密码选项,plist 应该检查他的用户名是否存在。如果存在,那么他输入的新密码应覆盖 plist 中的现有密码。

谁能帮我一些示例代码

4

2 回答 2

3

您不能更改存储在应用程序包中的 plist。如果您需要编辑存储在 plist 中的用户数据,那么您需要在文档目录上创建 plist。

另一个选项是NSUserDefaults。如果有大量数据,那么我会建议sqlite3数据库,否则您可以使用plistor NSUserDefaults

您可以更改 plist 的值,例如:

NSString *path = //your plist path in document directory;
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
NSString *userExist = [plistDict objectForKey:@"UserName"]; //test the user exist
if([userExist isEqualToString:@"Midhun"])                   //checking user exist or not
{
  NSString *userPwd = @"Midhun";
  [tempDict setObject:userPwd forKey:@"PassWord"]; //Password is your key
  [tempDict writeToFile:path atomically:YES];
}

请参阅此链接了解更多信息。这是为您准备的 plist教程

于 2012-10-22T11:11:26.887 回答
0

如果 plist 中的数据位于 bundle 目录中,则无法编辑它。捆绑包中存在的文件具有“只读”权限。如果您的 plist 位于文档目录中,则可以使用以下步骤对其进行编辑。(1)从plist中获取数据。(2) 更新它。(3)将更新后的数据写回plist。

NSString *path= [[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"plist"];

if(path)

 NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];

//Update the respective dictionary dict here.

//write back to plist.

[dict writeTofile:path atomically:YES];
于 2012-10-22T10:51:32.997 回答