Could not find files in bundle

During the development of the IOS application in Xcode4, I added a data folder to the application package by dragging the folder and, to get to this folder, I wrote:

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

However, I cannot reach folderPath and its contents.

+4
source share
2 answers

When you add a folder to the application, it does not save the structure / hierarchy of folders, it just adds files to the package. You need to remove the folder name from the code and it will work

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

hope this helps.

+2
source

Your code is a bit strange, why do UTF8String from the package path? You can simply use bundlePath directly in your format:

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

It would be more correct:

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

This will ensure that all the correct directory separators are added.

And if you use only one file:

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

Just make sure you add the folder as a folder, not a group for the project. If you selected groups, you can get the file path with:

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

Source: https://habr.com/ru/post/1489506/


All Articles