如何在plist中写入数据?
我已经按照这个答案写数据给plist了
如何将数据写入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
这是你如何初始化drinkArray属性:
NSString *destPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, 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];
- 在UIWebView中禁用点击延迟
- 如何从无符号字符创build图像*
- iOS开发:我怎样才能得到一个Facebook的墙上post显示在朋友的新闻提要?
- paymentquue updatedTransactions:没有调用,而是paymentQueueRestoreCompletedTransactionsFinished:调用
- 使用swift在UILabel中单击NSAttributedString事件
- 应用程序处于后台时未收到后台提取远程通知,并且用户未通过推送通知进入
- 通过自动布局在UIScrollView中使用顶层布局指南
- 将MKMapView指向像素坐标
- swift:如何从swift类调用overrided的Objective-C类的init方法?