srikanth rongali
srikanth rongali

Reputation: 1453

How can I retain the data stored in plist from UITextField when application is restarted?

I am using the plist to store the data entered in the UITextFields. But, when I restart my application all the data entered previously was deleted. How can I retain the data in the Plist. I have a UITableView and when a cell is touched a view appears with two UITextFields. nameField and descriptionField. I stored the data in this way.

My code is.

-(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 of the file is

/Users/srikanth/Library/Application Support/iPhone Simulator/User/Applications/31DEFEE7-A468-44BD-B044-53BEA3391C1A/Documents

But the problem is every time I restart the application the data is created in new plist file in new folder. So, how can I store the data in one plist ?

Thank You.

Upvotes: 2

Views: 616

Answers (1)

Laurent Etiemble
Laurent Etiemble

Reputation: 27909

The error is triggered by the fact that the array is too small; you try to insert an object at an index that is beyond the upper bound of the array.

If you want to keep both the index and the data, store the index in the dictionary:

NSDictionary *myDict = [[NSDictionary alloc] initWithObjectsAndKeys:@"value", string2, @"key", string1, @"index", [NSNumber numberWithInt:indexOfDataArray], nil];
[myArray addObject:myDict];

Upvotes: 1

Related Questions