3

在 Xcode4 中开发 IOS 应用程序期间,我通过拖动文件夹将数据文件夹添加到应用程序包中,以便到达我编写的该文件夹:

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

但是,我无法达到folderPath及其内容。

4

2 回答 2

2

Your code is a bit wierd, why make an UTF8String from the bundle path? You can just use the bundlePath directly in your format:

NSString *folderPath =[NSString stringWithFormat:@"%@/myDataFolder", bundlePath];

More correct would be:

NSString *folderPath = [bundlePath stringByAppendingPathComponent:@"myDataFolder"];

This will make sure that all the correct directory separator are added.

And if you are after just one file:

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

Just make sure you add the folder as a folder and not groups to you project. If you have selected groups then you can get the file path with :

NSString *folderPath = [[NSBundle mainBundle] pathForResource:@"somefile" ofType:@"sometype"];
于 2013-07-03T14:04:30.867 回答
1

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

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

希望它会有所帮助。

于 2013-07-03T13:59:44.740 回答