1

I'm trying to load a pdf that is stored in my app, here is the code that I have

import UIKit
import PDFKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let pdfView = PDFView(frame: UIScreen.main.bounds)
        let url = Bundle.main.url(forResource: "example", withExtension: "pdf")
        pdfView.document = PDFDocument(url: url!)
        self.view.addSubview(pdfView)
    }

When I am running in simulator, I am getting the error that fatal error: unexpectedly found nil while unwrapping an Optional value in the debug are it is showing that url is nil .Any idea how to resolve it? I am running swift 4 and iOS 11 as target

1
  • Check that "example.pdf" is actually copied in your project, accessible and has the right name and extension. Commented Sep 25, 2017 at 9:01

1 Answer 1

4

Please click the checkbox under Target Membership in File Inspector.

File Inspector - Target Membership

Sign up to request clarification or add additional context in comments.

1 Comment

Yes this was the problem, under target membership, one will see the App name and has to check that for working

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.