I am trying to add a new item to my plist. However, it gets overwritten every time i press save:
-(IBAction)save:(id)sender {
appDelegate = (JobTestAppDelegate*)[[UIApplication sharedApplication]delegate];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"Work.plist"];
NSMutableDictionary* newNote = [[NSMutableDictionary alloc] init];
NSMutableDictionary *set = [NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"", @"", nil] forKeys:[NSArray arrayWithObjects:@"Work Name", @"Work ID", nil]];
NSArray *work = [NSArray arrayWithObjects:set, nil];
int row = 0;
newNote = [appDelegate.job objectAtIndex:row];
[newNote setValue:work forKey:@"Work"];
[appDelegate.job writeToFile:fileName atomically:TRUE];
[newNote release];
[self dismissModalViewControllerAnimated:YES];
}
I don't know which part of the code is wrong. I've been trying to solve the problem for days.
EDIT: appDelegate.job - a mutable array in my main app delegate class to create the plist :
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"Work.plist"];
job = [[NSMutableArray alloc] initWithContentsOfFile:fileName];
[job writeToFile:fileName atomically:YES];
EDIT2:
I need to store the user input (NSArray*work) into the work array
Root (Array)
Item 0 (Dict) Name (String) Work (Array) Item 0 (Dict) Work Name (String) Work ID (String) Item 1 (Dict) Work Name (String) 开发者_JS百科 Work ID (String) Item 1 (Dict) Name (String) Analysis (Array) Item 0 (Dict) Work Name (String) Work ID (String) Item 1 (Dict) Work Name (String) Work ID (String)
Edit3: NSMutableDictionary* newNote = [[NSMutableDictionary alloc] init];
NSMutableDictionary *set = [NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"", @"", nil] forKeys:[NSArray arrayWithObjects:@"Work Name", @"Work ID", nil]];
NSArray *work = [NSArray arrayWithObjects:set, nil];
int row = item;
newNote = [appDelegate.job objectAtIndex:row];
[newNote setValue:work forKey:@"Work"];
//i think this is the line which i am having problem with.
//without this line my data in the plist gets overwritten,
//with this code it copies the who item and create a new item into my root array
[appDelegate.job addObject:newNote];
[appDelegate.job writeToFile:fileName atomically:TRUE];
[newNote release];
Thanks in Advance, Leanne
You are overwriting whatever is at job index zero with your new item. You are also allocating memory here:
NSMutableDictionary* newNote = [[NSMutableDictionary alloc] init];
Then reassigning something else to this variable here:
newNote = [appDelegate.job objectAtIndex:row];
If you are trying to add a new item, you should be doing something like:
NSMutableDictionary* newNote = [[NSMutableDictionary alloc] init];
[newNote setValue:work forKey:@"Work"];
[appDelegate.job addObject:newNote];
[appDelegate.job writeToFile:fileName atomically:TRUE];
You are then adding a new dictionary to your job array, and writing out the whole array.
EDIT: From your comments, it looks like newNote should be a dictionary, with a name and an array in it. So, you need to add a new object to the existing array - you are currently setting a single item array to the "work" key of this dictionary.
You need something like:
NSMutableDictionary* newNote = [appDelegate.job objectAtIndex:0];
NSArray *oldWork = [newNote objectForKey:@"Work"];
NSArray *newWork = [oldWork arrayByAddingObject:set];
[newNote setObject:newWork forKey:@"Work"];
精彩评论