2

Here's my code. I have a file "dreamGirls.pdf"

import UIKit
import PDFKit

var pdfView : PDFView!

    func createPDFViewer() {
        let path = Bundle.main.path(forResource: "dreamGirls", ofType: "pdf")
        let url = URL(fileURLWithPath: path!)
        let pdfDocument = PDFDocument(url:url)

        self.pdfView.document = pdfDocument
        self.pdfView.autoScales = true
        self.pdfView.displayMode = .singlePageContinuous
        self.pdfView.displayDirection = .vertical

        let width = self.view.frame.width
        let height = self.view.frame.height - 100
        self.pdfView.frame = CGRect(x: 0, y: 100, width: width, height: height)
        self.pdfView.backgroundColor = .red
        self.view.addSubview(self.pdfView)
    }

I also tried the following:

func createPDFViewer() {

    let fileToShow = Bundle.main.url(forResource: "dreamGirls", withExtension: "pdf")
    let documentToShow = PDFDocument(url: fileToShow!)

    self.pdfView.document = documentToShow
    self.pdfView.autoScales = true
    self.pdfView.displayMode = .singlePageContinuous
    self.pdfView.displayDirection = .vertical

    let width = self.view.frame.width
    let height = self.view.frame.height - 100
    self.pdfView.frame = CGRect(x: 0, y: 100, width: width, height: height)
    self.pdfView.backgroundColor = .red
    self.view.addSubview(self.pdfView)
}

Error output: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

pdfView PDFView? nil none

I also just tried this, and got the same error:


import UIKit
import PDFKit

class ViewController: UIViewController {

    var pdfView : PDFView?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        let fileToShow = Bundle.main.url(forResource: "dreamGirls", withExtension: "pdf")
        let documentToShow = PDFDocument(url: fileToShow!)

        self.pdfView!.document = documentToShow!
        self.pdfView!.autoScales = true
        self.pdfView!.displayMode = .singlePageContinuous
        self.pdfView!.displayDirection = .vertical

        let width = self.view.frame.width
        let height = self.view.frame.height - 100
        self.pdfView!.frame = CGRect(x: 0, y: 100, width: width, height: height)
        self.pdfView!.backgroundColor = .red
        self.view.addSubview(self.pdfView!)


    }


}

self pdfViewerTest.ViewController 0x00007fe538520580 UIKit.UIViewController UIViewController
pdfView PDFView? nil none fileToShow URL? "file:///Users/nacly/Library/Developer/CoreSimulator/Devices/C92B8B59-90C5-4509-A848-99D0B39A4469/data/Containers/Bundle/Application/7D0B63DF-F265-48DB-8887-D0563FD45FDA/pdfViewerTest.app/dreamGirls.pdf" some _url NSURL "file:///Users/nacly/Library/Developer/CoreSimulator/Devices/C92B8B59-90C5-4509-A848-99D0B39A4469/data/Containers/Bundle/Application/7D0B63DF-F265-48DB-8887-D0563FD45FDA/pdfViewerTest.app/dreamGirls.pdf" 0x0000600002638120 documentToShow PDFDocument? 0x00006000000301d0 ObjectiveC.NSObject NSObject
width CGFloat height CGFloat

enter image description here

I also tried the following, in case the PDFView wasn't initialized:


import UIKit
import PDFKit

class ViewController: UIViewController {

    var pdfView : PDFView?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.pdfView = PDFView()
        let fileToShow = Bundle.main.url(forResource: "dreamGirls", withExtension: "pdf")
        let documentToShow = PDFDocument(url: fileToShow!)

        self.pdfView!.document = documentToShow!
        self.pdfView!.autoScales = true
        self.pdfView!.displayMode = .singlePageContinuous
        self.pdfView!.displayDirection = .vertical

        let width = self.view.frame.width
        let height = self.view.frame.height - 100
        self.pdfView!.frame = CGRect(x: 0, y: 100, width: width, height: height)
        self.pdfView!.backgroundColor = .red
        self.view.addSubview(self.pdfView!)


    }

It returned the following error:

pdfViewerTest[16863:1994476] * Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan nan]' * First throw call stack:

Mason Ballowe
  • 1,847
  • 2
  • 12
  • 23
  • You need to watch your values for url and pdfDocument through your execution. One or both of them are null. SWIFT appears to me more than happy to unwrap path and/or url as null without an error. but when it force unwraps the whole thing as PDFView it's blowing up. I suspect the Bundle.main.path and .url are not returning exactly what you expect and may need some editing. – user11705765 Aug 02 '19 at 18:16
  • I've tried some guard/let stuff but it's still not working. – Mason Ballowe Aug 02 '19 at 18:36
  • I'm sure this example can be weeded down further. –  Aug 02 '19 at 21:55
  • How would you suggest I format it better? I have 4 clear attempts, with error messages, and even a screenshot of the error... what else can I do? – Mason Ballowe Aug 04 '19 at 15:14

1 Answers1

0

Ok, I solved it. I had to do the call in "viewDiDAppear" instead of "viewDiDLoad" because of bad geometry.

Mason Ballowe
  • 1,847
  • 2
  • 12
  • 23