我在對Resources/rchd/rchd.plist內(nèi)容進行操作..想在plist文件里面追加一條數(shù)據(jù).我現(xiàn)在的代碼如下:
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];
然后就是把要添加的數(shù)據(jù)放入到dictionary中咖祭,再保存dictionary济欢,寫入文件告希。不然 總是被覆蓋掉
追加的話先從沙盒里面讀取你原來的plist文件忽舟,然后再在后面追加數(shù)據(jù)诡右,而不是直接重新創(chuàng)建NSDictionary去覆蓋原來的數(shù)據(jù)。
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:LISTFILE(@"buy.plist")];
int count = [[dic objectForKey:@"count"] integerValue];
[dic setObject:[NSNumber numberWithLong:(count + 20)] forKey:@"count"];