Next Riot

Swift, Xcode, Cocoa & iOS thoughts, tips, tutorials & tricks by Kyle Gillen of Next Riot

Catch me on Twitter
Subscribe via RSS

Subscribe to my Swift Newsletter

Using folder references instead of inidividual files under ‘Copy Bundle Resources’ in Xcode to organise your layout

Posted on

The Why

I was getting pretty sick of seeing this layout under Targets > Build Phases > Copy Bundle Resources:

Bad organisation via copying entire folders to Xcode's sidebar

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:

Good organisation resulting from copying entire folders to Xcode's Copy Bundle Resources section directly

  • 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:

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 images folder:

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, images.