我一直在試圖將數據寫回到我的包中的預定義plist文件(data.plist)。使用下面的代碼,我調用例程'dictionaryFromPlist'來打開文件,然後調用'writeDictionaryToPlist'來寫入plist文件。但是,沒有數據被添加到plist文件。將數據添加到plist文件的問題
NSDictionary *dict = [self dictionaryFromPlist];
NSString *key = @"Reports";
NSString *value = @"TestingTesting";
[dict setValue:value forKey:key];
[self writeDictionaryToPlist:dict];
- (NSMutableDictionary*)dictionaryFromPlist {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
NSMutableDictionary* propertyListValues = [[NSMutableDictionary alloc]
initWithContentsOfFile:filePath];
return [propertyListValues autorelease];
}
- (BOOL)writeDictionaryToPlist:(NSDictionary*)plistDict{
NSString *filePath = @"data.plist";
BOOL result = [plistDict writeToFile:filePath atomically:YES];
return result;
}
代碼成功運行,沒有錯誤拋出,但沒有數據添加到我的plist文件。
謝謝兩位,上面的代碼工作完美。 – user616076