IOS正在plist中创建词典列表
我很难在plist文件中编写新的词典,每次用户填写表单时,我都想保存数据。 我现在使用的代码只会覆盖数据,不会按需要保存以前的字典 代码如下:IOS正在plist中创建词典列表,ios,plist,Ios,Plist,我很难在plist文件中编写新的词典,每次用户填写表单时,我都想保存数据。 我现在使用的代码只会覆盖数据,不会按需要保存以前的字典 代码如下: //////////// Save data into plist ///////////////// // get paths from root direcory NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, Y
//////////// 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:但这会导致应用程序崩溃。嗯,崩溃可能是因为您过度使用了错误变量。读取plistPath的内容:
NSMutableData*myData=[NSMutableData dataWithContentsOfFile:plistPath]
改变这一点:
// create NSData from dictionary
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
致:
最后将myData写入文件
希望有帮助 你从哪里得到这个错误?如果没有保留错误局部变量,为什么要释放它?
// create NSData from dictionary
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
[myData appdendData:plistData];