Edit an existing data in plist

10,583

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.

Share:
10,583
Vamsi
Author by

Vamsi

Updated on June 04, 2022

Comments

  • Vamsi
    Vamsi about 2 years

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

    In Detail:

    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