简体   繁体   中英

Unable to save data in Plist file

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];
    }
}

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

检查我的截图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM