Bundle.main.path(forResource:ofType:inDirectory:) returns nil Bundle.main.path(forResource:ofType:inDirectory:) returns nil ios ios

Bundle.main.path(forResource:ofType:inDirectory:) returns nil


The issue is that the file isn't being copied to your app bundle. To fix it:

  • Click your project
  • Click your target
  • Select Build Phases
  • Expand Copy Bundle Resources
  • Click '+' and select your file.


Double check the Options in the add files menu when adding the file. The target in Add to targets must be ticked to add it to the bundle:

In case you are actually in another bundle (test for instance), use:

guard let fileURL = Bundle(for: type(of: self)).url(forResource: fileName withExtension:"txt") else {        fatalError("File not found")}


Click on your file on your navigation panel and open the Right Panel/ Property Inspector.

enter image description here

Ensure that you add to target membership