为什么 plist 中的项目会被覆盖?
我正在尝试向我的 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)存储到工作数组中
根(数组)
项目 0(字典) 名称(字符串) 工作(数组) 项目 0(字典) 作品名称(字符串) 工作 ID(字符串) 第 1 项(字典) 作品名称(字符串) 工作 ID(字符串) 第 1 项(字典) 名称(字符串) 分析(阵列) 项目 0(字典) 作品名称(字符串) 工作 ID(字符串) 第 1 项(字典) 作品名称(字符串) 工作 ID(字符串)
编辑3: 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];
提前致谢, 莉安
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) 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在用新项目覆盖工作索引为零的任何内容。您还可以在此处分配内存:
然后在此处为该变量重新分配其他内容:
如果您尝试添加新项目,则应该执行以下操作:
然后,您将向作业数组添加一个新字典,并写出整个字典大批。
编辑:从您的评论来看, newNote 看起来应该是一本字典,其中有一个名称和一个数组。因此,您需要向现有数组添加一个新对象 - 您当前正在将单个项目数组设置为此字典的“work”键。
你需要类似的东西:
You are overwriting whatever is at job index zero with your new item. You are also allocating memory here:
Then reassigning something else to this variable here:
If you are trying to add a new item, you should be doing something like:
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: