简体   繁体   中英

Bundle.main.url cannot find file

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM