为什么 plist 中的项目会被覆盖?

发布于 2024-12-01 01:04:54 字数 3214 浏览 0 评论 0原文

我正在尝试向我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨后咖啡店 2024-12-08 01:04:55

您正在用新项目覆盖工作索引为零的任何内容。您还可以在此处分配内存:

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"];

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"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文