2013-05-13 74 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