-1

I tried several answers from different posts but none of them solved my problem.

Situation I am in:

  • The project is called XcodeTests and the only target it has is XcodeTests
  • I have a my_file.txt file at <ProjectRoot>/XcodeTests/res/
  • Upon creating my_file.txt, I made sure that target XcodeTests was selected
  • Both res folder and my_file.txt exists in storage directories confirmed by Finder
  • In Build Phase of XcodeTests, CopyBundleResources does include my_file.txt

The following code throws Fatal error: Couldn't find requested file:

import Foundation

guard let myFileURL = Bundle.main.url(forResource: "my_file", withExtension: "txt")
else
{
    fatalError("Couldn't find requested file")
}

What should I do so that Bundle.main.url can find my_file.txt?

Jaxon
  • 164
  • 9
  • 1
    Well your file is inside a directory called res. try `Bundle.main.url(forResource: "my_file", withExtension: "txt", subdirectory: "res")` – Leo Dabus Nov 26 '22 at 22:56
  • I added`, subdirectory: "res"`. It still cannot find `my_file.txt`. – Jaxon Nov 26 '22 at 23:02
  • What color is your subdirectory? I guess it is not blue. It is probably yellow. Possible duplicate of [Swift - How do I get the file path inside a folder](https://stackoverflow.com/a/34548888/2303865) – Leo Dabus Nov 26 '22 at 23:03
  • It's grey. I think it's the color for group folders. – Jaxon Nov 26 '22 at 23:08
  • check my comment above again – Leo Dabus Nov 26 '22 at 23:08
  • Just print out the contents of your bundle like this: https://stackoverflow.com/a/26571579/5318223 and see if the file is there, and if it has parent folder – timbre timbre Nov 27 '22 at 00:54
  • @akjndklskver Thanks for that info. I `cd`ed into `Bundle.main.bundlePath` and realized this target is a command line program. Even tho Xcode allows copy resources to bundle and this "bundle" does have a valid address, no resources actually get copied. – Jaxon Nov 27 '22 at 12:49

1 Answers1

-1

Turns out it's because the target itself is a command line program and does not support Copy Bundle Resources. Even tho resources can be listed there, none of them will be copied.

Jaxon
  • 164
  • 9