Я пытаюсь добавить новый элемент в мой список. Тем не менее, он перезаписывается каждый раз, когда я нажимаю сохранить:
-(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];
}
Я не знаю, какая часть кода неправильная. Я пытался решить проблему в течение нескольких дней.
EDIT:
appDelegate.job - изменяемый массив в моем основном классе делегатов приложения для создания 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:
Мне нужно сохранить пользовательский ввод (NSArray * work) в рабочий массив
Root (Array)
Item 0 (Dict)
Name (String)
Work (Array)
Item 0 (Dict)
Work Name (String)
Work ID (String)
Item 1 (Dict)
Work Name (String)
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];
Заранее спасибо,
Лиан