Objective c 将一些数据附加到plist

Objective c 将一些数据附加到plist,objective-c,plist,Objective C,Plist,我正在创建一个允许你记录棋盘游戏的应用程序。我正在将日志存储在plist中,并试图找出如何附加plist。我想我理解逻辑,但我在把它编成代码方面遇到了困难 我的plist看起来是这样的: 现在我正在尝试类似的东西,但我相信它只会覆盖plist文件,而不是附加 //get path for root directory NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask

我正在创建一个允许你记录棋盘游戏的应用程序。我正在将日志存储在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];
任何帮助都将不胜感激。

像这样试试

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);
NSString*path=[[NSBundle mainBundle]pathForResource:@“plist”类型的“数据”;
NSDictionary*dict=[NSDictionary Dictionary WithContentsOfFile:path];
NSLog(@“dictbeforepending..%@”,dict);
//正在获取上一个数组。。。。。
NSMutableArray*prevArray=[[NSMutableArray alloc]init];
prevArray=[[dict valueForKey:@“Root”]valueForKey:@“Logs”];
NSLog(@“prevArray..%@”,prevArray);
//将新内容添加到旧内容。。。。。
NSMUTABLEARRY*players=[[NSMUTABLEARRY alloc]initWithObjects:@“NewPlayer1”、“Newplayer2”、“Newplayer3”、nil]//1.
NSDictionary*newObject=[NSDictionary Dictionary WithObjects:[NSArray arrayWithObjects:@“LogTille2”、@“Krish”、玩家、@“它会更有趣”、无]福克斯:[NSArray arrayWithObjects:@“Log Title”、@“Name”、@“Player”、@“Notes”、无]//2.
NSMutableArray*newArray=[[NSMutableArray alloc]init];
[newArray addObject:newObject];

对于(int i=0;为了允许修改对象,您必须将数据存储在可变对象中,如
NSMutableArray
NSMutableDictionary
等。而不是
NSArray
NSDictionary
。我希望它足够清晰谢谢!这让我朝着写入的方向前进。因此,当我从我的plist sho读取数据时uld我可以从捆绑包中访问它,还是应该访问这个临时字典?这似乎是晚些时候,因为我在日志中读取到表的视图只显示了我在plist中已有的数据,而没有新数据。我还可以在第一次运行代码时为prevArray获取null。谢谢!如果你想在documentdirectory中,只需将你的plist复制到它,你就可以了在重新加载tableview时追加。最初,如果您没有在plist中设置任何值,则表示plist中没有任何内容,那么它怎么不能为null?亲爱的,它应该为null。。