Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 无法写入Plist_Iphone_Plist - Fatal编程技术网

Iphone 无法写入Plist

Iphone 无法写入Plist,iphone,plist,Iphone,Plist,我有一个非常基本的问题,关于如何给一个plist写信。我在我的资源文件夹中添加了一个“Here.plist”文件。以下是我的代码: NSString *path = [[NSBundle mainBundle]pathForResource:@"Here" ofType:@"plist"]; NSArray *array = [[NSArray alloc]initWithObjects:@"First", @"Second", @"Third", nil]; [array writeToF

我有一个非常基本的问题,关于如何给一个plist写信。我在我的资源文件夹中添加了一个“Here.plist”文件。以下是我的代码:

NSString *path = [[NSBundle mainBundle]pathForResource:@"Here" ofType:@"plist"];

NSArray *array = [[NSArray alloc]initWithObjects:@"First", @"Second", @"Third", nil];

[array writeToFile:path atomically:YES];
执行代码后,“Here.plist”仍然为空。我甚至试着使用NSD字典,如下所示:

NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:@"firstValue", @"First", @"secondValue", @"Second", @"thirdValue", @"Third", nil];

[dict writeToFile:path atomically:YES];

我看了很多教程和论坛,但对我来说都不管用。我做错了什么?这让我不知所措。

你不能在包里写东西。尝试写入文档目录。例如:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *array = [[NSArray alloc]initWithObjects:@"First", @"Second", @"Third", nil];
[array writeToFile:[documentsDirectory stringByAppendingPathComponent:@"Here.plist" atomically:YES];

你不能在包裹里写东西。尝试写入文档目录。例如:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *array = [[NSArray alloc]initWithObjects:@"First", @"Second", @"Third", nil];
[array writeToFile:[documentsDirectory stringByAppendingPathComponent:@"Here.plist" atomically:YES];

我怀疑您的问题是因为您无法写入应用程序包,只能写入应用程序的文档存储

要获取文档存储的路径,请使用:

NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docStorePath = [searchPaths objectAtIndex:0];

NSString *filePath = [docStorePath stringByAppendingPathComponent:@"XXXXX"];

我怀疑您的问题是因为您无法写入应用程序包,只能写入应用程序的文档存储

要获取文档存储的路径,请使用:

NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docStorePath = [searchPaths objectAtIndex:0];

NSString *filePath = [docStorePath stringByAppendingPathComponent:@"XXXXX"];

嘿,谢谢,成功了。但有一个问题,为什么不将这些值添加到我的资源文件夹中的plist中?正在将这些值添加到/users/name/library/application支持。。。文件夹。无法写入资源文件夹。部分原因是您的整个捆绑包(包括resources文件夹)已签名。对捆绑包的任何更改都将使签名无效,应用程序将无法运行。Documents目录是存储安装后创建的文件或安装后将更改的文件的位置。打包应用程序时,需要包括resources目录中的所有文件。也就是说,在应用程序被签名之前,saym.Ohk。谢谢。被困在这个问题上太久了。嘿,谢谢,这很有效。但有一个问题,为什么不将这些值添加到我的资源文件夹中的plist中?正在将这些值添加到/users/name/library/application支持。。。文件夹。无法写入资源文件夹。部分原因是您的整个捆绑包(包括resources文件夹)已签名。对捆绑包的任何更改都将使签名无效,应用程序将无法运行。Documents目录是存储安装后创建的文件或安装后将更改的文件的位置。打包应用程序时,需要包括resources目录中的所有文件。也就是说,在应用程序被签名之前,saym.Ohk。谢谢。被困在这个问题上太久了,这就成功了。但是为什么我的项目中的plist没有得到更新呢。它已添加到应用程序支持文件夹中的plist中。为什么?这就成功了。但是为什么我的项目中的plist没有得到更新呢。它已添加到应用程序支持文件夹中的plist中。这是为什么??