0

I have a name textfield, and three textfields for phone numbers like homePhone, workPhone and cellPhone. When I click on save button to save the data to plist file, it does't work. Please help in this regard.

My code is as follows:

Save Button code in .m file

- (IBAction)btnSave:(id)sender
{
  NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsPath = [paths objectAtIndex:0];

    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];

    // set the variables to the values in the text fields
    self.personName = nameEntered.text;
    self.phoneNumbers = [[NSMutableArray alloc] initWithCapacity:3];
    [phoneNumbers addObject:homePhone.text];
    [phoneNumbers addObject:workPhone.text];
    [phoneNumbers addObject:cellPhone.text];

    // create dictionary with values in UITextFields
    NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: personName, phoneNumbers, nil] forKeys:[NSArray arrayWithObjects: @"Name", @"Phones", nil]];

    NSString *error = nil;
    // create NSData from dictionary
    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];

    // check is plistData exists
    if(plistData)
    {
        // write plistData to our Data.plist file
        [plistData writeToFile:plistPath atomically:YES];
    }
    else
    {
        NSLog(@"Error in saveData: %@",error);
        //[error release];
    }
}
Mudassir
  • 71
  • 11
  • I have taken the code from http://www.theappcodeblog.com/?tag=ios-saving-data-to-plist-tutorial – Mudassir Jan 15 '16 at 07:07
  • Looks like your data have some 'nil' values. I executed the same above code by passing static text , its working fine. Posting your code as my answer check it. – AskIOS Jan 15 '16 at 07:10
  • Hey @Mudasir Please Check this link http://stackoverflow.com/questions/905542/how-to-write-data-in-plist – Mihir Oza Jan 15 '16 at 07:10

1 Answers1

0

I executed your code by passing static values to dictionary and array. Its working fine. There might be some nil values in your data. That might causing crash.

NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); // get documents path
NSString *documentsPath = [paths objectAtIndex:0];
NSString * personName = @"Bharath";
NSMutableArray * phoneNumbers = [[NSMutableArray alloc] initWithCapacity:3];
[phoneNumbers addObject:@"123"];
[phoneNumbers addObject:@"123"];
[phoneNumbers addObject:@"123"];

NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: personName, phoneNumbers, nil] forKeys:[NSArray arrayWithObjects: @"Name", @"Phones", nil]];

NSString *error = nil;
// create NSData from dictionary
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];

// check is plistData exists
if(plistData)
{
    // write plistData to our Data.plist file
    [plistData writeToFile:plistPath atomically:YES];
    NSLog(@"done");
}
else
{
    NSLog(@"Error in saveData: %@",error);
    //[error release];
}

plist data is, Name Bharath Phones 123 123 123

Check my screenshot

AskIOS
  • 918
  • 7
  • 19
  • Copy the above code and paste to your method and call that. Its saved data.plist file with data. – AskIOS Jan 15 '16 at 07:24