SwiftUI集成Realm Apple登录触发未识别选择器异常求助
异常原因及解决办法
核心原因
这个异常是Swift端调用的方法签名与Kotlin共享代码的实际方法签名不匹配导致的,具体体现在两个地方:
- Kotlin的
loginwithApple是suspend挂起函数,仅接收appleTokenIDString参数并返回User,没有定义回调闭包;但Swift调用时额外传入了一个completionHandler闭包,导致跨语言绑定工具试图寻找不存在的带回调参数的方法。 - 异常信息里的方法名
loginwithAppleAppleTokenIDString:是错误拼接的结果,说明Swift端生成的调用签名完全没对应上Kotlin的方法。
解决步骤
修正Swift端调用方式:
Kotlin的suspend函数在Swift中会被转换为支持async/await的异步方法,不需要传入闭包,改成异步调用:case .success(let authResults): guard let credentials = authResults.credential as? ASAuthorizationAppleIDCredential, let identityToken = credentials.identityToken, let identityTokenString = String(data: identityToken, encoding: .utf8) else { return } Task { do { let user = try await repo.loginwithApple(appleTokenIDString: identityTokenString) // 处理登录成功逻辑 isLoggingIn = false } catch { // 处理登录失败逻辑 isLoggingIn = false } }检查Kotlin方法的暴露配置:
确保Kotlin方法正确标记异常抛出,方便Swift端捕获:@Throws(Exception::class) suspend fun loginwithApple(appleTokenIDString: String): User { val credentials = Credentials.apple(idToken = appleTokenIDString) return appService.login(credentials = credentials) }清理并重新编译项目:
执行Xcode的Clean Build Folder操作,重新生成KMM的跨语言绑定代码,避免旧签名缓存导致的不匹配问题。
内容的提问来源于stack exchange,提问作者dhaval123
相关产品推荐
相关产品推荐

