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

SwiftUI DropDelegate中用loadObject提取URL遇CONTINUATION MISUSE错误求助

问题分析

你遇到的SWIFT TASK CONTINUATION MISUSE错误,核心原因是**loadObject的回调存在未处理分支,导致continuation没有被正确resume**:

  • 当loadObject返回错误时,你的代码未调用continuation.resume,任务延续被泄漏
  • 另外,loadObject回调的object实际是Any?类型,直接当作URL?处理存在类型隐患
修复后的代码
func extractURL(info: DropInfo) async -> URL? {
    return await withCheckedContinuation { continuation in
        guard let firstProvider = info.itemProviders(for: [.url]).first else {
            continuation.resume(returning: nil)
            return
        }
        
        firstProvider.loadObject(ofClass: URL.self) { object, error in
            // 处理错误、类型转换失败等所有异常情况,确保continuation被resume
            guard let url = object as? URL else {
                continuation.resume(returning: nil)
                return
            }
            continuation.resume(returning: url)
        }
    }
}
关键修复点
  • 确保loadObject回调的所有分支都调用了continuation.resume,包括错误、类型转换失败的场景
  • 显式将回调中的object转换为URL?,规避隐式类型转换的风险
  • 使用guard let提前退出分支,让代码逻辑更清晰直观

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 05:52:38