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

Objective-C代码未执行if-else分支,求排查原因

QLPreviewController canPreviewItem执行后直接跳转到方法末尾的原因排查

问题现象

代码执行到BOOL canOpenFile = [QLPreviewController canPreviewItem:file];这一行后,直接跳转到方法末尾的} //last line,未进入后续if-else分支。

涉及代码

RCT_EXPORT_METHOD(open:(NSString *)path invocation:(nonnull NSNumber *)invocationId
    options:(NSDictionary *)options)
{
    NSString *displayName = [RCTConvert NSString:options[@"displayName"]];
    DyteFile *file = [[File alloc] initWithPath:path title:displayName];

    QLPreviewController *controller = [[CustomQLViewController alloc] initWithFile:file identifier:invocationId];
    controller.delegate = self;

    typeof(self) __weak weakSelf = self;
    BOOL canOpenFile = [QLPreviewController canPreviewItem:file];
    if(canOpenFile) {
        [[RNFileViewer topViewController] presentViewController:controller animated:YES completion:^{
            [weakSelf sendEventWithName:OPEN_EVENT body: @{@"id": invocationId}];
        }];
    } else {
        [weakSelf sendEventWithName:OPEN_EVENT body: @{@"id": invocationId, @"error": @"File not supported"}];
    }
} //last line

可能的原因

  • canPreviewItem:触发未捕获异常:QLPreviewController的类方法在处理DyteFile实例时,若DyteFile未正确实现QLPreviewItem协议的必需方法(如previewItemURL/previewItemTitle),或返回的URL无效、文件不存在,可能触发崩溃级异常,导致当前方法直接终止。
  • DyteFile实例为nil:如果[[File alloc] initWithPath:path title:displayName]初始化失败返回nil,调用[QLPreviewController canPreviewItem:nil]时,系统内部逻辑错误会直接终止方法执行。
  • 线程违规:QLPreviewController的方法要求在主线程执行,若当前代码在后台线程调用canPreviewItem:,会触发线程违规导致的隐式崩溃,使方法直接结束。
  • 内存问题:CustomQLViewController或DyteFile初始化时出现野指针、内存越界等问题,会在canPreviewItem:执行时触发异常终止。

内容的提问来源于stack exchange,提问作者Alwaysblue

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 14:55:27