2011-08-22 39 views
0

我想添加一個新項目給我的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]; 

由於事先 李亞男

+0

什麼是appDelegate.job?你可以發佈一些與其操作相關的代碼嗎? – Akshay

+0

感謝您的及時回覆(:我有alr編輯我的問題,併發布appDelegate.job代碼。你知道什麼是缺少或錯誤的代碼?謝謝(: – Leanne

回答

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應該是一個字典,一個名稱和一個數組。所以,你需要添加一個新的對象到現有的數組中 - 你正在設置一個單一的數組作爲這個字典的「工作」鍵。

你需要的東西,如:

NSMutableDictionary* newNote = [appDelegate.job objectAtIndex:0]; 
NSArray *oldWork = [newNote objectForKey:@"Work"]; 
NSArray *newWork = [oldWork arrayByAddingObject:set]; 
[newNote setObject:newWork forKey:@"Work"]; 
+0

感謝您的答覆(:上面的代碼將保存我的代碼作爲我的根數組中的一個項目,但是我想將它存儲到我的工作數組(這是一個子/子項)顯示在上面的plist。我如何編輯我的代碼,使其保存到工作數組中的新項目,而不是覆蓋它?非常感謝(: – Leanne

+0

查看更新回答 – jrturton

+0

謝謝(:但代碼確實有效,我知道我的代碼的哪個部分發生了錯誤,我已經編輯過了,你能幫我看一下。謝謝 – Leanne