编辑plist中的现有数据 [英] Edit an existing data in plist

查看:65
本文介绍了编辑plist中的现有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can i override an existing data in a plist after checking if the data is already present or not, through coding

详细信息:

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

I am saving username and password in my plist file located in document directory. Now if user selects an change password option, the plist should check if his username exists or not. if it exists then the new password that he is entering should override the existing password in the plist.

有人可以帮我提供一些示例代码

can anyone help me with some piece of sample code

推荐答案

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

You can't change the plist stored in the application bundle. If you need to edit the user data stored in the plist, then you need to create the plist on the document directory.

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

Another option is NSUserDefaults. If there is huge data then I'll suggest sqlite3 database, else you can use plist or NSUserDefaults.

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

You can alter the value of your plist like:

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 教程.

Please refer this link for more. Here is a plist tutorial for you.

这篇关于编辑plist中的现有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆