0

In my app I have a folder named "CSS" with file named "style.css" in it.
When I try to load it, I get this error :

Fatal error: Unexpectedly found nil while unwrapping an Optional value

This is the code where I am trying to load it :

let path = Bundle.main.path(forResource: "style", ofType: "css", inDirectory: "CSS")!
let cssString = try! String(contentsOfFile: path).trimmingCharacters(in: .whitespacesAndNewlines)
3
  • Does this answer your question? What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean? Commented Feb 10, 2020 at 17:11
  • 1 - make sure you included CSS/style.css (case-sensitive) in resource bundle at build time; 2 - never force-unwrap, check for errors instead with if or guard, e.g. guard let path = Bundle.main.path(...) Commented Feb 10, 2020 at 17:17
  • The problem was in CSS folder. Everything works when I remove my style.css from CSS folder. Commented Feb 11, 2020 at 8:49

1 Answer 1

2

Ensure your file is present in the Build Phases > Copy Bundle Resources list

dont force unwrap , check for errors like this

   guard Bundle.main.path(forResource: "style", ofType: "CSS") != nil else {
        return
    }

    do {

    } catch let error {
        print(error)
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.