我無法在我的plist文件中編寫新字典,我希望每次用戶填寫表單時都要保存數據。 我現在使用的代碼只覆蓋數據,並不會保存以前的字典爲想要的。IOS在plist中創建字典列表
這裏的代碼:
//////////// Save data into plist /////////////////
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];
// set the variables to the values in the text fields
self.titlestring = titleexperiencia.text;
self.descriptionstring = descriptionexperiencia.text;
self.coordinadastring = [self deviceLocation];
// create dictionary with values in UITextFields
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: titlestring, descriptionstring,coordinadastring, nil] forKeys:[NSArray arrayWithObjects: @"Title", @"Description", @"Coordinate", nil]];
NSString *error = nil;
// create NSData from dictionary
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
// check is plistData exists
if(plistData)
{
// write plistData to our Data.plist file
[plistData writeToFile:plistPath atomically:YES];
NSLog(@"Saved in Data Plist");
}
else
{
NSLog(@"Error in saveData: %@", error);
[error release];
}
我還試圖用ADDOBJECT:不是將writeToFile:但使應用程序崩潰。
你從哪裏得到錯誤?如果你沒有保留它,你爲什麼要釋放錯誤局部變量? – 2012-08-07 12:24:29