我想添加一個新項目給我的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的*作)進入工作陣列
根(陣列)
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?你可以發佈一些與其操作相關的代碼嗎? – Akshay
感謝您的及時回覆(:我有alr編輯我的問題,併發布appDelegate.job代碼。你知道什麼是缺少或錯誤的代碼?謝謝(: – Leanne