plist文件追加数据

我在对Resources/rchd/rchd.plist内容进行操作..想在plist文件里面追加一条数据.我现在的代码如下:
NSString *path = [[NSBundle mainBundle] pathForResource:@"rchd" ofType:@"plist"];
NSLog(@"path=%@",path);

NSMutableDictionary* dict = [ [ NSMutableDictionary alloc ] initWithContentsOfFile:path ];

[ dict setObject:@"Yes" forKey:@"titleKey" ];

[ dict setObject:@"315.jpg" forKey:@"imageKey" ];

[ dict setObject:@"0" forKey:@"stateKey" ];

[ dict setObject:@"2012-12-24" forKey:@"timeKey" ];

[ dict writeToFile:path atomically:YES ];

[dict release];

dictionary= [[nsmutabledictionary alloc ] initwithcontentsoffile:path];

然后就是把要添加的数据放入到dictionary中,再保存dictionary,写入文件。不然 总是被覆盖掉

追加的话先从沙盒里面读取你原来的plist文件,然后再在后面追加数据,而不是直接重新创建NSDictionary去覆盖原来的数据。

NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:LISTFILE(@"buy.plist")];

int count = [[dic objectForKey:@"count"] integerValue];

[dic setObject:[NSNumber numberWithLong:(count + 20)] forKey:@"count"];

你可能感兴趣的:(plist文件追加数据)