Home > Mobile >  Bundle.main.url cannot find file
Bundle.main.url cannot find file

Time:11-29

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?

CodePudding user response:

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.

  • Related