Eugene Karambirov
Eugene Karambirov

Reputation: 343

Get nil when looking for file in subdirectory of main bundle

I've copied a folder with index.html to the project.

When I try to get a path to the file inside a folder, I get nil.

let path = Bundle.main.path(forResource: "index", ofType: "html", inDirectory: "games/game1")

I also tried to use "/games/game1" string. Doesn't work though.

But when I move index.html to the root of my project and use

let path = Bundle.main.path(forResource: "index", ofType: "html")

I get a correct path.

So my question is how do I get the path of index.html inside a subfolder?

Upvotes: 9

Views: 8860

Answers (4)

Alessio Campanelli
Alessio Campanelli

Reputation: 1030

using Xcode 16, make sure you have Build rules on Apply Once to Folder on root folder (containing files) and then this folder has correct Target Membership. You can find this setting in right side 'Identity and Type'

Using Bundle.main.url(forResource:withExtension:subdirectory) in fact, if you don't have correct setting on folder and file, subdirectory param doesn't work.

Upvotes: 1

Mumtaz Hussain
Mumtaz Hussain

Reputation: 1125

Alternatively, adding to Eugene's answer,

Swift 5:

Bundle.main.url(forResource: "index", withExtension: "html", inDirectory: "games/game1")

Upvotes: 2

Eugene Karambirov
Eugene Karambirov

Reputation: 343

The solution is to use

Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "games/game1")

I don't know why, but it doesn't work if we get a path, then construct an URL from it.

Note: You must

  1. Copy items if needed
  2. Create folder references (not "Create groups")
  3. Add to targets

Upvotes: 10

Vinay Kiran
Vinay Kiran

Reputation: 347

Please refer Apple Doc link

In summary
Apple uses bundles to represent apps, frameworks, plug-ins, and many other specific types of content. Bundles organize their contained resources into well-defined subdirectories, and bundle structures vary depending on the platform and the type of the bundle. By using a bundle object, you can access a bundle's resources without knowing the structure of the bundle. The bundle object provides a single interface for locating items, taking into account the bundle structure, user preferences, available localizations, and other relevant factors.

Bundle structures vary depending on the target platform and the type of bundle you are building. The Bundle class hides this underlying structure in most (but not all) cases. Many of the methods you use to load resources from a bundle automatically locate the appropriate starting directory and look for resources in known places. You can also use the methods and properties of this class to get the location of known bundle directories and to retrieve resources specifically from those directories.

You can add the index.html file anywhere in the project, but while fetching the use

let path = Bundle.main.path(forResource: "index", ofType: "html")

Upvotes: -2

Related Questions