Edit an existing data in plist

本秂侑毒 提交于 2019-12-04 12:17:33

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.

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

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];
}

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

You can't edit a data in plist if it is in your bundle directory. Files that are present inside the bundle have "read only" permission. While if your plist reside inside the document directory then you can edit it by using following steps. (1)Get the data from plist. (2)Update it. (3)Write back the updated data to 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];
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!