How do I keep the data stored in the plist from the UITextField when the application is restarted?
I am using plist to store the data entered into UITextFields. But when the application was restarted, all the data entered earlier was deleted. How to save data to Plist. I have a UITableView and when a cell is touched a view appears with two UITextFields. nameField and descriptionField. I saved the data this way.
My code.
-(void)save:(id)sender
{
indexOfDataArray = temp;
NSString *string1 = [[NSString alloc]init];
NSString *string2 = [[NSString alloc]init];
string1 = nameField.text;
string2 = descriptionField.text;
NSDictionary *myDict = [[NSDictionary alloc] initWithObjectsAndKeys:string2, string1, nil];
//NSDictionary *myDict = [[NSDictionary alloc] initWithObjectsAndKeys:@"value", string2, @"key", string1, @"index", [NSNumber numberWithInt:indexOfDataArray], nil];
[myArray addObject:myDict];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"tableVideoData.plist"];
[myArray writeToFile:path atomically:YES];
UIAlertView *alertMesage = [[UIAlertView alloc] initWithTitle: @"Save Alert" message:@"The data entered is saved" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:nil] ;
[alertMesage show];
[alertMesage release];
}
The path to the file
/Users/srikanth/Library/Application Support/iPhone Simulator/User/Applications/31DEFEE7-A468-44BD-B044-53BEA3391C1A/Documents
But the problem is that every time I restart the application, the data is created in a new plist file in a new folder. So how can I store data in a single plist?
Thanks.
+2
a source to share
1 answer
The error is caused by the array being too small; you are trying to insert an object at an index that is outside the top of the array.
If you want to store both the index and the data, store the index in a dictionary:
NSDictionary *myDict = [[NSDictionary alloc] initWithObjectsAndKeys:@"value", string2, @"key", string1, @"index", [NSNumber numberWithInt:indexOfDataArray], nil];
[myArray addObject:myDict];
+1
a source to share