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

KMP-NativeCoroutines未为iOS生成asyncFunction/asyncResult方法问题

KMP-NativeCoroutines中asyncFunction/asyncSequence/asyncResult找不到的问题解决

项目已引入KMP-NativeCoroutines库,能正常获取标记为@NativeCoroutinesState的Flow初始值,但调用挂起函数或订阅Flow时,执行以下代码会报错Cannot find 'asyncFunction' in scope,asyncSequence和asyncResult也存在相同问题:

let handle = Task {
    do {
        let data = try await asyncFunction(for: service.getData())
        print("Got data: \(data)")
    } catch {
        print("Failed with error: \(error)")
    }
}

以下是解决步骤:

  • 验证Kotlin端代码生成配置:在KMP模块的build.gradle.kts(或build.gradle)中,确保已启用KMP-NativeCoroutines插件,并开启对应辅助函数的生成开关:
    plugins {
        id("com.rickclephas.kmp.nativecoroutines") version "你的库版本"
    }
    
    kmpNativeCoroutines {
        ios {
            generateAsyncFunctions = true
            generateAsyncSequences = true
            generateAsyncResults = true
        }
    }
    
  • 确认Kotlin函数类型与注解:挂起函数无需额外注解即可生成asyncFunction;Flow类型需确保返回值正确,若要生成asyncSequence,无需额外注解但需保证配置中generateAsyncSequences为true。同时检查service.getData()确实是挂起函数或Flow类型的返回值。
  • 清理并全量重构项目:清理Kotlin构建缓存,重新编译KMP模块,再同步Xcode项目并重新构建iOS端,避免缓存导致代码生成不完整。
  • 检查iOS端模块导入:在Swift文件顶部确认已正确导入Kotlin生成的模块,例如import YourKotlinModule,确保辅助函数属于该模块的可见范围。
  • 核对版本兼容性:确保KMP-NativeCoroutines版本与项目中Kotlin、Kotlin Multiplatform版本匹配,版本不兼容会导致代码生成失败。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 12:53:23