0

I have a growing collection of files in my Swift project which I want to organise better. This includes a folder of sounds, a few font files and my Localizable.strings file.

I also have a Resources folder which is empty, because every time I move a file into it the project can’t find it any more and the application bombs.

For example, when referencing a sound file, I can use Bundle.main.path(forResource: "thing", ofType: "mp3")! which finds it in my Sounds folder at the root level, but not if I move it into the Resources folder. Ditto the font files, and I haven’t had the courage to try my strings file.

What is the trick to moving files into subfolders?

Edit:

Somebody decided that this was answered in Swift - How do I get the file path inside a folder. Well, no, but the accepted answer did at least point me to inDirectory which is the new name for subdirectory; I included Resources/Sounds and it works.

However, it doesn’t answer the question of the font files.

Manngo
  • 14,066
  • 10
  • 88
  • 110
  • 1
    You should ask only one question at a time – Leo Dabus Oct 20 '20 at 01:29
  • @LeoDabus I thought I did. The way I see it is that the problem with moving, say, my fonts into the `Resources` folder is that same as the problem of moving my `Sounds` folder there. With sounds, I can force the issue with `inDirectory`, but I don’t see that option with fonts or other resources. I already include `Resources` in `Copy Bundle Resources`, but that’s not doing the job. – Manngo Oct 20 '20 at 01:34
  • AFAIK the fonts directory should be added to the info.plist – Leo Dabus Oct 20 '20 at 01:37
  • @LeoDabus OK, I’ll give it ago. Thanks – Manngo Oct 20 '20 at 01:38
  • https://stackoverflow.com/a/27681982/2303865 – Leo Dabus Oct 20 '20 at 01:40
  • @LeoDabus That last link has fixed my font problem, and I see that I can’t do anything with `Localizable.strings` anyway, so that’s it. Thanks again for your help. – Manngo Oct 20 '20 at 01:56

1 Answers1

0

I'm assuming you have a group (yellow folder) in the project navigator with these files and you want these files copied to the application bundle when the project builds. What you have to do is add these files to the Copy Bundle Resources build phase for your app target.

Select your project from the project navigator to open the project editor. Select your app from the target list and click the Build Phases button at the top of the project editor to see the build phases.

Copy Bundle Resources build phase

Click the Add (+) button to add the Sounds folder, the fonts, and the strings file to the Copy Bundle Resources build phase. When you build your project, these files will be copied to the Resources folder inside the app bundle.

Swift Dev Journal
  • 19,282
  • 4
  • 56
  • 66