2013-05-13 43 views
0

我正在創建一個應用程序,允許您記錄棋盤遊戲。我將日誌存儲在plist中,並試圖找出如何追加plist。我想我理解這個邏輯,但是我很難將它放入代碼中。將一些數據附加到plist

我的plist看起來像這樣:

plist

現在我想是這樣的,但我相信它會直接覆蓋,而不是附加

//get path for root directory 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 

NSLog(@"paths: %@",paths); 

//get path for documents directory 
NSString *documentsPath = [paths objectAtIndex:0]; 
NSLog(@"documentsPath: %@",documentsPath); 

//get the path for logs.plist file 
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"logs.plist"]; 


//create dictionary with values 
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:myLogTitle, name, players, myNotes, nil] forKeys:[NSArray arrayWithObjects:@"Log Title",@"Name",@"Players","Notes", nil]]; 

//write out data 
[plistDict writeToFile:plistPath atomically:YES]; 

任何幫助的plist文件會非常感激。

+0

http://stackoverflow.com/questions/4779877/how-to-write-in -append-mode-for-text-file – Balu 2013-05-13 04:16:24

+0

要允許在對象中進行修改,您必須將數據存儲在可變對象中,例如'NSMutableArray','NSMutableDictionary'等等,而不是'NSArray'或'NSDictionary'。我希望它足夠清晰 – 2013-05-13 05:20:38

回答

0

嘗試像這樣

NSString *path = [[NSBundle mainBundle] pathForResource: @"data" ofType: @"plist"]; 
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: path]; 
NSLog(@"dictbeforeAppending..%@",dict); 

//getting Previous Array..... 
NSMutableArray *prevArray = [[NSMutableArray alloc]init]; 
prevArray = [[dict valueForKey:@"Root"] valueForKey:@"Logs"]; 
NSLog(@"prevArray..%@",prevArray); 

// Add new stuff to old stuff..... 

NSMutableArray *players =[[NSMutableArray alloc]initWithObjects:@"NewPlayer1",@"Newplayer2",@"Newplayer3", nil];//1 
NSDictionary *newObject = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Logtiltle2", @"Krish", players, @"it will be more fun", nil] forKeys:[NSArray arrayWithObjects:@"Log Title",@"Name",@"Players",@"Notes", nil]];//2 

NSMutableArray *newArray = [[NSMutableArray alloc]init]; 
[newArray addObject:newObject]; 
for (int i=0;i<[prevArray count];i++){ 
    [newArray addObject:[prevArray objectAtIndex:i]]; 
} 

//set all values for Plist...... 
NSMutableDictionary *allItemsDict = [[NSMutableDictionary alloc]init]; 
NSMutableArray *Logs = [[NSMutableArray alloc]initWithArray:newArray]; 
[allItemsDict setObject:Logs forKey:@"Logs"]; 

NSMutableDictionary *Root = [[NSMutableDictionary alloc]init]; 
[Root setObject:allItemsDict forKey:@"Root"]; 

// Now, write to the plist: 
[Root writeToFile:path atomically:YES]; 

NSDictionary *dictafterAppending = [NSDictionary dictionaryWithContentsOfFile: path]; 
NSLog(@"dictafterAppending..%@",dictafterAppending); 

,如果你停留在任意位置,那麼請讓我知道豬頭...

+0

謝謝!這讓我以我認爲的寫作方向爲嚮導。因此,當我從我的plist讀取數據時,是否應該從包中獲取它,或者我應該訪問這個temp字典? 看來是後者,因爲我的看法是將我的日誌讀入表中,只顯示我已經在plist中獲得的數據,並且沒有新的數據。 – 2013-05-13 13:48:45

+0

第一次運行代碼時,我還得到了prevArray的null值。謝謝! – 2013-05-13 14:44:39

+0

如果你想在文檔目錄中,然後將你的plist複製到它,使用dictafterAppending而重新加載tableview。 – iSpark 2013-05-14 04:36:48