【发布时间】:2011-08-22 09:27:53
【问题描述】:
我正在尝试向我的 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)存储到工作数组中
根(数组)
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)
编辑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];
提前致谢, 莉安娜
【问题讨论】:
-
什么是 appDelegate.job?你能发布一些与它的操作相关的代码吗?
-
感谢您的及时回复(:我已经编辑了我的问题并发布了 appDelegate.job 的代码。您知道我的代码中缺少什么或错误吗?谢谢(:
标签: iphone objective-c ios nsarray plist