1

I'm writing some Swift code that opens PDF files inline using a 3rd party control. The problem is, for a small (but non-negligible) portion of PDF documents, the control encounters an "unexpectedly found nil" due to certain metadata being missing from the PDF document. Apparently try/catch only works in swift for errors that are explicitly declared by the programmer - nevertheless here is the code I've tried (which warns that no exception is thrown)

    var document: PDFDocument
    do{
        // exception is happening in this constructor
        try document = PDFDocument(filePath: location.path, password: nil)
    }catch{
        // fallback to using iOS defaults
        let av = UIAlertController(title: "Error", message: "This document couldn't be opened.  You will now be directed to your phone's default application for handling this file.", preferredStyle: .alert)
        av.addAction(UIAlertAction(title: "OK", style: .default, handler: {(alert: UIAlertAction!) in
            let docController = UIDocumentInteractionController(url: location)
            ...
        }))
        return;
    }

Approaches I've considered:

  • Try/catch - but the catch block is never run due to reasons mentioned above
  • Override the 3rd party code - my most promising option, but due to the way the code is written the override would be pretty gnarly and I'd like to avoid it if possible
  • Manipulate the PDF document on the fly to not throw the exception - maybe, but might be expensive for large PDF files

Is there a way to arbitrarily catch errors like these? Or is an override the best approach?

I'm fairly new to Swift so it may be my ignorance, but this seems like a pretty big gap compared to other platforms I've used.

Edit: For what its worth, the 3rd party library is something called UXMPDFKit

Thanks,

  • I'm not using Swift, but in order to attract more help, you may want to give us the name of your third library code (and if it's open source, give us the source), and potentially a pdf which is making it crash. Also, if the author of the third library didn't manage all the case, "it's its fault". if you have access to the code, maybe pointing out the issue and giving him a forgotten case could help. – Larme Sep 25 '17 at 15:24
  • Thanks - I've updated my post and will let the developer know. I'm hoping not to have to wait for the bug fix turnaround time. So an approach that I can use in my own code would be ideal. – Daniel Moore Sep 25 '17 at 15:33
  • Post indeed an issue on the GitHub. Also, you can help by trying to identify which line exactly is causing the issue (for here also if other can help you with a work around). – Larme Sep 25 '17 at 15:38
  • So to clarify, the `unexpectedly found nil...` error is thrown __inside__ a 3rd party library function? If so, there is no way to get around that issue, it is clearly a programming error on the library developers side, so you cannot do anything other than submitting a GitHub issue and waiting for them to sort this out. – Dávid Pásztor Sep 25 '17 at 16:12
  • Yes, that's correct - inside the 3rd party code. Yes, that's kind of what I was afraid of. I guess there isn't much I can do other than wait. Very strange though - any other platform I've used it seems you can always be safe by wrapping your calls to 3rd party code in try/catches and gracefully handle any problems. Thanks anyway. – Daniel Moore Sep 25 '17 at 16:22
  • Not all errors can be caught in Swift. Check there: https://stackoverflow.com/questions/35331353/swift-2-1-do-try-catch-not-catching-error – Larme Sep 25 '17 at 22:03

0 Answers0