Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.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
Objective c 无法在捆绑包中找到文件_Objective C - Fatal编程技术网

Objective c 无法在捆绑包中找到文件

Objective c 无法在捆绑包中找到文件,objective-c,Objective C,在Xcode4中开发IOS应用程序期间,我通过拖动文件夹将数据文件夹添加到应用程序包中,为了访问此文件夹,我写了: NSString *bundlePath = [[NSBundle mainBundle] bundlePath]; NSString *folderPath =[NSString stringWithFormat:@"%s/myDataFolder",([bundlePath UTF8String]),nil]; 但是,我无法访问folderPath及其内容 将文件夹添加到应用

在Xcode4中开发IOS应用程序期间,我通过拖动文件夹将数据文件夹添加到应用程序包中,为了访问此文件夹,我写了:

NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *folderPath =[NSString stringWithFormat:@"%s/myDataFolder",([bundlePath UTF8String]),nil];

但是,我无法访问
folderPath
及其内容

将文件夹添加到应用程序时,它不会保留文件夹的结构/层次结构,只会将文件添加到捆绑包中。你必须从代码中删除你的文件夹名,它才能工作

NSString *folderPath =[NSString stringWithFormat:@"%s/",([bundlePath UTF8String]),nil];

希望能有所帮助。

您的代码有点复杂,为什么要从包路径生成UTF8String?您可以直接以您的格式使用
bundlePath

NSString *folderPath =[NSString stringWithFormat:@"%@/myDataFolder", bundlePath];
更正确的做法是:

NSString *folderPath = [bundlePath stringByAppendingPathComponent:@"myDataFolder"];
这将确保添加了所有正确的目录分隔符

如果您只需要一个文件:

NSString *folderPath = [[NSBundle mainBundle] pathForResource:@"somefile" ofType:@"sometype" inDirectory:@"myDataFolder"];

只需确保将文件夹作为文件夹而不是组添加到项目中即可。如果选择了组,则可以通过以下方式获取文件路径:

NSString *folderPath = [[NSBundle mainBundle] pathForResource:@"somefile" ofType:@"sometype"];

@特洛伊木马程序,编辑问题,拖动整个文件夹检查
myDataFolder
是否确实在应用程序捆绑包中。捆绑包中没有文件夹。只需访问名称为的文件。@Desdenova是否可用取决于您如何将文件添加到项目中。如果您选择了“创建组”,则选择了“是”;如果您选择了“创建文件夹”,则您的包中有文件夹。@rckoenes我将对此进行研究。谢谢哇,我不知道捆绑包没有保存文件夹,谢谢。@yucelbayram您的说法不正确,您可以将文件夹添加到应用程序捆绑包中。在项目中拖动文件夹时,只需选择“添加为文件夹”,而不是“组”。然后文件夹将添加到您的应用程序包中。