大佬教程收集整理的这篇文章主要介绍了ios – 如何用plist写入数据?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
How to write data to the plist?
但到目前为止,我的plist根本没有变化.
这是我的代码: –
- (IBAction)save:(id)sender { NSString *path = [[NSBundle mainBundle] pathForResource:@"drinks" ofType:@"plist"]; NSString *drinkName = self.name.text; NSString *drinkIngredients = self.ingredients.text; NSString *drinkDirection = self.directions.text; NSArray *values = [[NSArray alloc] initWithObjects:drinkDirection,drinkIngredients,drinkName,nil]; NSArray *keys = [[NSArray alloc] initWithObjects:DIRECTIONS_KEY,INGREDIENTS_KEY,NAME_KEY,nil]; NSDictionary *dict = [[NSDictionary alloc] initWithObjects:values forKeys:keys]; [self.drinkArray addObject:dict]; NSLog(@"%@",self.drinkArray); [self.drinkArray writeToFile:path atomically:YES]; }
我需要执行额外的操作吗?
我是iPhone SDK的新手,所以任何帮助将不胜感激.
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) lastObject]; path = [path stringByAppendingPathComponent:@"drinks.plist"];
pathForResource方法只能用于读取您在Xcode中添加到项目中的资源.
以下是您在应用中修改plist时通常执行的操作:
1.首次启动时,将drinks.plist从应用程序包复制到应用程序的Documents文件夹(使用NSFileManager).
2.读/写时仅使用Documents文件夹中的文件.
UPDATE
NSString *destPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,YES) lastObject]; destPath = [destPath stringByAppendingPathComponent:@"drinks.plist"]; // If the file doesn't exist in the Documents Folder,copy it. NSFileManager *fileManager = [NSFileManager defaultManager]; if (![fileManager fileExistsAtPath:destPath]) { NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"drinks" ofType:@"plist"]; [fileManager copyItemAtPath:sourcePath toPath:destPath error:nil]; } // Load the Property List. drinkArray = [[NSArray alloc] initWithContentsOfFile:destPath];
以上是大佬教程为你收集整理的ios – 如何用plist写入数据?全部内容,希望文章能够帮你解决ios – 如何用plist写入数据?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。