Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.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存储并读取文档文件夹中的文件_Iphone_Objective C_File - Fatal编程技术网

iphone存储并读取文档文件夹中的文件

iphone存储并读取文档文件夹中的文件,iphone,objective-c,file,Iphone,Objective C,File,这必须是容易的,但我想把一个文件在文件文件夹,这是读在启动。我有关于如何阅读的代码,并且已经确认它在正确的目录中。但是,我的文件RootList.txt保存在xcode的Resources文件夹中时,存储在Root.app文件夹下,而Documents文件夹是空的。因此,当我启动应用程序时找不到此文件 有没有办法确保在启动时将文件内置到Documents目录中?我正在模拟器中运行这个 另一种选择是plist,它也可以很好地工作,但我只是好奇。在这些情况下,我遵循以下方法: 首先将RootList

这必须是容易的,但我想把一个文件在文件文件夹,这是读在启动。我有关于如何阅读的代码,并且已经确认它在正确的目录中。但是,我的文件RootList.txt保存在xcode的Resources文件夹中时,存储在Root.app文件夹下,而Documents文件夹是空的。因此,当我启动应用程序时找不到此文件

有没有办法确保在启动时将文件内置到Documents目录中?我正在模拟器中运行这个


另一种选择是plist,它也可以很好地工作,但我只是好奇。

在这些情况下,我遵循以下方法:

首先将RootList.txt保存在xCode的Resources文件夹中。您的Documents文件夹中还没有任何内容

在ApplicationIDLaunch调用开始时,执行以下操作:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [docsDirectory stringByAppendingPathComponent:@"RootList.txt"];
if(![fileManager fileExistsAtPath:path])
{
NSData *data = [NSData dataWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/RootList.txt"]];
[data writeToFile:path atomically:YES];
}

现在,您的文件在启动时位于Documents文件夹中。

在这些情况下,我采用以下方法:

首先将RootList.txt保存在xCode的Resources文件夹中。您的Documents文件夹中还没有任何内容

在ApplicationIDLaunch调用开始时,执行以下操作:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [docsDirectory stringByAppendingPathComponent:@"RootList.txt"];
if(![fileManager fileExistsAtPath:path])
{
NSData *data = [NSData dataWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/RootList.txt"]];
[data writeToFile:path atomically:YES];
}
现在,您的文件在启动时位于Documents文件夹中