views:

148

answers:

1

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.

+1  A: 

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];
Laurent Etiemble
Thank you.But how can I retain the data in pList. It is deleted every time I restart the application.
srikanth rongali
Why don't you use NSUserDefaults?
Macmade
Macmade is right. You can use NSUserDefaults if you need persistence for your data. See http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html
Laurent Etiemble
I used the NSUserDefaults but the data is not stored in the plist name I specified.
srikanth rongali
NSUserDefaults cannot be used to be stored in a file. It is a system-managed store, for storing preferences.If you want to save your data in a file then remove all the NSUserDefault stuff, because your code seems fine.A couple of questions:- When you quit the application, is the file still there ?- Is the file disappearing when you start the application ?
Laurent Etiemble
I observed the following every time I run my application.Each time a new folder is created in the path /Users/srikanth/Library/Application Support/iPhone Simulator/User/Applications/ And the newly entered data in is stored in the new folder.newFolder/Documents/tableVideoData.plist. The file is still there it is not disappearing.How can I have all the data entered in one plist.Thank you for correcting my code.
srikanth rongali
Sorry, for my confusion. Each time I run the application new folder is not created but, the data in the folder (plist data)is replaced by new plist data. When I restart my system a new folder is created each time . But the data in the folder is there,(not deleted) even I restart my system.
srikanth rongali
How do you load the data ?
Laurent Etiemble
After entering the data in the text fields. I click save button. And the function for save is in above code(in question). I am accessing the data from text fields and stored in dictionary and then added to an array. Then using writeToFile I am writing it to plist in the path specified.Thank You.
srikanth rongali