Unable to load file from bundle

Hi there,

I downloaded this code from hackingwithswift.com, but it always fails at loading the file. Does anyone have an idea what might cause this problem? Here's my code:
Code Block swift
if let filepath = Bundle.main.path(forResource: "german", ofType: "dic") {
      do {
        contents = try String(contentsOfFile: filepath)
        print(contents)
      } catch {
        print("contents could not be loaded")
      }
    } else {
      print("file not found")
    }
      text = contents.components(separatedBy: "\n")


Answered by OOPer in 621689022
  • The file german.dic is surely added to your project? (File names are case-sensitive in iOS.)

  • Does it exist in the flat place? (Meaning not under a Folder which is bundled as a directory)

  • The Target Membership is checked? (Shown in the File Inspector at the right hand of Xcode window.)

Accepted Answer
  • The file german.dic is surely added to your project? (File names are case-sensitive in iOS.)

  • Does it exist in the flat place? (Meaning not under a Folder which is bundled as a directory)

  • The Target Membership is checked? (Shown in the File Inspector at the right hand of Xcode window.)

I ran into this exact same issue. The file was in my bundle, I could see this when running my iOS app on my simulator and using Finder to inspect the app bundle. If I switched String(contentsOfFile: String) to String(contentsOf: URL) it worked.

-Ed

Unable to load file from bundle
 
 
Q