PdfView 中的 PdfDocument 因 Objective C 中无法识别的选择器实例而崩溃

PdfDocument in PdfView is crash with unrecognised selector instance in Objective C

我正在使用 Xcode 9.4 和 Objective C。 我想按照以下代码使用 PDFView 显示 pdf 文件

PDFView * pdfView = [[PDFView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
NSString* path = [[NSBundle mainBundle] pathForResource:@"abc" ofType:@"pdf"];
NSData* data = [NSData dataWithContentsOfFile:path];
PDFDocument *pdfDocument = [[PDFDocument alloc] initWithData:data];
[pdfView setDocument:pdfDocument];

但它因 SetDocument 无法识别的选择器实例以及以下错误而失败。

错误:

-[PDFView setDocument:]: 无法识别的选择器发送到实例 0x101367c40

错误:执行被中断,原因:试图取消引用无效的 ObjC 对象或向其发送无法识别的选择器。 进程已经返回到表达式求值前的状态。

我无法确定确切的问题,请帮助。

谢谢。

根据 Apple 的 PDFView 文档,有一个 document 属性 保存 PDF 文档。不要调用 setDocument,而是将 document 属性 设置为您的 pdfDocument 变量。

最后,

我得到了解决方案, 只需在故事板中添加 UIView 并在 Inspector 中将 class 设置为 PDFView 即可。 并创建 PDFView 的 IBOutlet 对象并将其分配给情节提要中的 UIView 并根据需要将 pdfdocument 连同属性一起设置。

代码:

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Swift" ofType:@"pdf"]];
    PDFDocument *document = [[PDFDocument alloc]initWithURL:url];
    _pdfView.document = document;
    _pdfView.frame = self.view.frame;
    _pdfView.displayDirection = kPDFDisplayDirectionHorizontal;

感谢大家为讨论付出的宝贵努力。

我遇到了同样的问题,对我来说,原因是另一位开发人员已将 pod "UIImage-PDF" 添加到我们的项目中,该项目还提供了一个名为 class 的 PDFView,但界面不同。

将此答案放在这里以防其他人对这个问题的根本原因与我相同。