编辑plist中的现有数据

时间:2012-10-22 10:44:58

标签: ios xcode plist

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

详细信息:

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

任何人都可以帮我处理一些示例代码

2 个答案:

答案 0 :(得分:3)

您无法更改应用程序包中存储的plist。 如果需要编辑存储在plist中的用户数据,则需要在文档目录中创建plist。

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

您可以更改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];
}

请参阅此link了解更多信息。 这是你的tutorial。{/ p>

答案 1 :(得分:0)

如果数据位于包目录中,则无法编辑plist中的数据。 捆绑包中存在的文件具有“只读”权限。 如果您的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];