我正在尝试向我的plist添加一个新项目。但是,每次按保存时它都会被覆盖:
-(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];
}
我不知道代码的哪一部分是错的。我一直试图解决这个问题好几天。
编辑: 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(数组)
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];
先谢谢, 李亚男
答案 0 :(得分:0)
您正在使用新项目覆盖作业索引为零的任何内容。你也在这里分配内存:
NSMutableDictionary* newNote = [[NSMutableDictionary alloc] init];
然后在此处为此变量重新分配其他内容:
newNote = [appDelegate.job objectAtIndex:row];
如果您尝试添加新项目,则应执行以下操作:
NSMutableDictionary* newNote = [[NSMutableDictionary alloc] init];
[newNote setValue:work forKey:@"Work"];
[appDelegate.job addObject:newNote];
[appDelegate.job writeToFile:fileName atomically:TRUE];
然后,您将新的字典添加到作业数组,并写出整个数组。
编辑:从你的评论看,newNote看起来应该是一个字典,里面有一个名字和一个数组。因此,您需要向现有数组添加新对象 - 您当前正在将单个项目数组设置为此字典的“work”键。
您需要以下内容:
NSMutableDictionary* newNote = [appDelegate.job objectAtIndex:0];
NSArray *oldWork = [newNote objectForKey:@"Work"];
NSArray *newWork = [oldWork arrayByAddingObject:set];
[newNote setObject:newWork forKey:@"Work"];