You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Objective-C中QLPreviewController不存在dataSource属性的报错求助

iOS QuickLook框架报错:Property 'dataSource' not found on object of type 'QLPreviewController *'

问题场景

开发iOS应用时使用QuickLook框架展示PDF文档,运行时报错:Property 'dataSource' not found on object of type 'QLPreviewController *',核心报错代码行:

qlController.dataSource = self.pdfDataSource

完整业务代码片段:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    /*
     *  get the path to the pdf resource.
     */
    
    NSString *path =
    [[NSBundle mainBundle] pathForResource:@"article"
                                    ofType:@"pdf"];
    NSURL *docURL = [NSURL fileURLWithPath:path];
    
    
    /*
     *  create the Quicklook controller.
     */
    QLPreviewController *qlController = [[QLPreviewController alloc] init];
    
    PreviewItem *item =
    [[PreviewItem alloc] initPreviewURL:docURL
                              WithTitle:@"Article"];
    self.pdfDatasource =
    [[PDFDataSource alloc] initWithPreviewItem:item];
    
    qlController.dataSource = self.pdfDataSource;
    
    
    /*
     *  present the document.
     */
    
    [self presentViewController:qlController
                       animated:YES completion:nil];
}

注:代码取自旧开发指南,因限制无法切换至Swift,需基于Objective-C解决问题。

解决方案

1. 修正变量名拼写错误

代码中变量名存在大小写不一致:

  • 赋值时用的是self.pdfDatasource(小写s)
  • 赋值给控制器时用的是self.pdfDataSource(大写S)
    统一变量名拼写即可,例如都改为self.pdfDataSource:
self.pdfDataSource = [[PDFDataSource alloc] initWithPreviewItem:item];
qlController.dataSource = self.pdfDataSource;

2. 确保数据源类遵循QLPreviewControllerDataSource协议

PDFDataSource类必须明确遵循QLPreviewControllerDataSource协议,并实现协议要求的两个核心方法:

// PDFDataSource.h 文件
#import <QuickLook/QuickLook.h>

@interface PDFDataSource : NSObject <QLPreviewControllerDataSource>
- (instancetype)initWithPreviewItem:(id<QLPreviewItem>)item;
@end

// PDFDataSource.m 文件
@implementation PDFDataSource
{
    id<QLPreviewItem> _previewItem;
}

- (instancetype)initWithPreviewItem:(id<QLPreviewItem>)item {
    self = [super init];
    if (self) {
        _previewItem = item;
    }
    return self;
}

// 实现协议方法:返回预览项数量
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
    return 1;
}

// 实现协议方法:返回对应索引的预览项
- (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
    return _previewItem;
}

@end

3. 确认导入QuickLook头文件

在使用QLPreviewController的类文件顶部,确保导入正确的框架头文件:

#import <QuickLook/QuickLook.h>

4. 适配iOS版本(可选)

如果是针对iOS 13及以上版本,也可以直接使用previewItems属性简化代码,无需单独创建数据源类:

QLPreviewController *qlController = [[QLPreviewController alloc] init];
qlController.previewItems = @[item]; // item需遵循QLPreviewItem协议
[self presentViewController:qlController animated:YES completion:nil];

内容的提问来源于stack exchange,提问作者Nguyen Thien Minh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 23:35:23