I was getting pretty sick of seeing this layout under
Targets > Build Phases > Copy Bundle Resources:
This was a result of simply dragging and dropping the images folder (and/or files) directly into Xcode’s sidebar. Note the color of the
images folder within the sidebar – it’s manilla.
However, rather than dragging and dropping the folder/files directly to the sidebar, if instead you drag the folder directly from finder into the
Copy Bundle Resources section directly, you’ll notice the following:
- Firstly, rather than a bunch of individual items under
Copy Bundle Resources, there’s simply a folder.
- Secondly, the folder in the sidebar is blue. My assumption for that is that in this scenario the folder is referenced to, rather than copied over.
Which brings me to a caveat, ensure your folder structure and resources are as you want them within your project directory prior to taking these steps, unless your idea of fun is resolving missing binary links.
So what’s the point?
So I’ve quietened the OCD demon within me, but where’s the practical application? Why add these extra steps? Well, personally the organisational factor was a huge win in and of itself I thought, but there is another, more practical reason:
func pathsForResourcesOfType(ext: String?, inDirectory subpath: String?) -> [String]
Returns an array containing the pathnames for all bundle resources having the specified filename extension and residing in the resource subdirectory.
pathsForResourcesOfType mirrors the folder structure seen under
Copy Bundle Resources. In the first, non-organised example in this post, if you attempted the following code you would be left with an empty array (
) as the compiler is seeing individual files and knows nothing of the
let images: [String] = NSBundle.mainBundle().pathsForResourcesOfType("png", inDirectory: "images")
In the second, organised example on the other hand you’d be given back an array of every path with a file that matches the
.png extension within the specified directory,