-1

hi i have set up a table view to display entires in a plist and have a section to add new data to the plist but i am unable to save the data... it stores fine and display fine but when i leave the app and reload the new data has gone here is how im saving

- (IBAction) save: (id) sender {
NSLog(@"%@", @"Save pressed!");

if (self.job != nil) {
    // We're working with an existing drink, so let's remove
    // it from the array and just recreate it like we would a new
    // drink...
    [jobArray_ removeObject:self.job];
    self.job = nil; // This will release our reference and set the property to nil
}

// Create a new drink dictionary for the new values
NSMutableDictionary *newDrink = [[NSMutableDictionary alloc] init];
[newDrink setValue:self.nameTextField.text forKey:NAME_KEY];
[newDrink setValue:self.ingredientsTextView.text forKey:INGREDIENTS_KEY];
[newDrink setValue:self.directionsTextView.text forKey:DIRECTIONS_KEY];

// Add it to the master drink array and release our reference
[jobArray_ addObject:newDrink];

// Sort the array since we just added a new drink
NSSortDescriptor *nameSorter = [[NSSortDescriptor alloc] initWithKey:NAME_KEY ascending:YES selector:@selector(caseInsensitiveCompare:)];
[jobArray_ sortUsingDescriptors:[NSArray arrayWithObject:nameSorter]];

// Pop the modal view and go back to the list
[self dismissViewControllerAnimated:YES completion:NULL];
}

am i making a mistake for it not to save to the plist? it loads the data i preset in the plist but not the new entrys

thanks in advance

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
Azabella
  • 837
  • 2
  • 10
  • 18

1 Answers1

1

Please save the data in pList like this :

    NSMutableDictionary *dict;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentPath = [paths objectAtIndex:0];

    NSString *plistPath = [documentPath stringByAppendingPathComponent:@"myplist.plist"];

    NSLog(@"File path = %@", plistPath);

    NSFileManager *fileManager  =   [NSFileManager defaultManager];

    BOOL success = [fileManager fileExistsAtPath:plistPath];

    // This line saves the dictionary to pList
    [dict writeToFile:plistPath atomically:YES];
Samkit Jain
  • 2,523
  • 16
  • 33