我正在创建一个iPhone应用程序,其中我得到所有国家的名称,标志和参赛者姓名.我想保存该数据在.plist而不是sqlite服务器.我不知道如何在DocumentDirectory中创建一个plist文件并保存数据.
请有人建议我如何保存plist文件中的数据.
解决方法
我将通过截图进行一步一步的处理.请遵循这一点,你会得到你的答案.
步骤1
第2步
步骤:3
保存按钮上的数据操作:
// Take 3 array for save the data ..... -(IBAction)save_Action:(id)sender { NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory,NSUserDomainMask,YES); NSString *documentsPath = [paths objectAtIndex:0]; NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"manuallyData.plist"]; [self.nameArr addObject:self.nameField.text]; [self.countryArr addObject:self.countryField.text]; [self.imageArr addObject:@"image.png"]; NSDictionary *plistDict = [[NSDictionary alloc] initWithObjects: [NSArray arrayWithObjects: self.nameArr,self.countryArr,self.imageArr,nil] forKeys:[NSArray arrayWithObjects: @"Name",@"Country",@"Image",nil]]; NSString *error = nil; NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error]; if(plistData) { [plistData writeToFile:plistPath atomically:YES]; alertLbl.text = @"Data saved sucessfully"; } else { alertLbl.text = @"Data not saved"; } } // Data is saved in your plist and plist is saved in DocumentDirectory
步骤4
从plist检索数据文件:
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory,YES); NSString *documentsPath = [paths objectAtIndex:0]; NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"manuallyData.plist"]; if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) { plistPath = [[NSBundle mainBundle] pathForResource:@"manuallyData" ofType:@"plist"]; } NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath]; self.nameArr = [dict objectForKey:@"Name"]; self.countryArr = [dict objectForKey:@"Country"];
步骤:5
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory,YES); NSString *documentsPath = [paths objectAtIndex:0]; NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"manuallyData.plist"]; NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:(NSString *)plistPath]; self.nameArr = [dictionary objectForKey:@"Name"]; self.countryArr = [dictionary objectForKey:@"Country"]; [self.nameArr removeObjectAtIndex:indexPath.row]; [self.countryArr removeObjectAtIndex:indexPath.row]; [dictionary writeToFile:plistPath atomically:YES];
步骤:6
更新您的数据更新单击操作:
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory,YES); NSString *documentsPath = [paths objectAtIndex:0]; NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"manuallyData.plist"]; if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) { plistPath = [[NSBundle mainBundle] pathForResource:@"manuallyData" ofType:@"plist"]; } self.plistDic = [[NSDictionary alloc] initWithContentsOfFile:plistPath]; [[self.plistDic objectForKey:@"Name"] removeObjectAtIndex:self.indexPath]; [[self.plistDic objectForKey:@"Country"] removeObjectAtIndex:self.indexPath]; [[self.plistDic objectForKey:@"Image"] removeObjectAtIndex:self.indexPath]; [[self.plistDic objectForKey:@"Name"] insertObject:nameField.text atIndex:self.indexPath]; [[self.plistDic objectForKey:@"Country"] insertObject:countryField.text atIndex:self.indexPath]; [[self.plistDic objectForKey:@"Image"] insertObject:@"dhoni.jpg" atIndex:self.indexPath]; [self.plistDic writeToFile:plistPath atomically:YES];