Nam
Nam

Reputation: 163

EXC_BAD_ACCESS If an error occurs, tell us how to try catch

let path = DataManager.Data.filePath
self.pdfDocument = PDFDocument(url: URL(fileURLWithPath: path))!

self.pdfView.document = self.pdfDocument   <——— EXC_BAD_ACCESS Error on this line.

EXC_BAD_ACCESS If an error occurs, tell us how to try catch.

It does not occur when opening all pdf files, but when trying to open a specific pdf file, EXC_BAD_ACCESS occurs.

First, when EXC_BAD_ACCESS occurs, you want to prevent app crush with try catch.

I have tried all the methods but I have not been able to catch it and I have been app crushing on that line.

What should I do..?

Upvotes: 0

Views: 268

Answers (1)

H.Epstein
H.Epstein

Reputation: 731

the error occures because the path you are trying to pass to the pdfDocument is nil and you try to force unwrap it ,

try to use this error handling

let path = DataManager.Data.filePath
if let document =  PDFDocument(url: URL(fileURLWithPath: path)) {
self.pdfView.document = document 
}
else {
 //return from function or do what ever you need if you can't get the pdf document
} 

Upvotes: 1

Related Questions