-[FBLPromise HTTPBody]:非初始启动时发送到实例 0x600001afa700 错误的无法识别的选择器。谷歌翻译 MLKit

Mcr*_*ich 5 firebase swift swiftui google-mlkit

我正在尝试将 Google MLKit Translate 添加到我的 SwiftUI 项目中。我已经通过 SPM 使用 firebase,并且仅在首次启动后才出现此错误:-[FBLPromise HTTPBody]: unrecognized selector sent to instance 0x600001afa700

这是我的代码:

应用程序委托

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        ...
        FirebaseApp.configure()
        ...
        let spanishRemoteTranslator = TranslateRemoteModel.translateRemoteModel(language: .spanish)
        if ModelManager.modelManager().isModelDownloaded(spanishRemoteTranslator) {
            print("Spanish Translator Downloaded")
        }else {
            print("Downloading Spanish Translator")
            
            ModelManager.modelManager().download(spanishRemoteTranslator, conditions: ModelDownloadConditions(allowsCellularAccess: true, allowsBackgroundDownloading: true))
        }
        return true
}
Run Code Online (Sandbox Code Playgroud)

然后我这样称呼它:

if ModelManager.modelManager().isModelDownloaded(spanishModel) {
    Translator.translator(options: englishSpanishTranslator).translate(buis.name!) { translatedText, error in
        if error == nil {
            if let translatedText = translatedText {
                name = translatedText
            }else {
                print("error = \(error)")
            }
        }else {
            print("error = \(error)")
        }
    }
}else {
    print("error = Spanish not downloaded")
}
Run Code Online (Sandbox Code Playgroud)

我还尝试过使用内置的 FirebaseMLKitDownload,但它没有翻译器。到底是怎么回事?

小智 3

这篇文章的作者可能找到了这个问题的解决方案(根据评论判断),但我决定写这篇文章,因为几个小时前,在使用 Firebase 的第一步中,我遇到了同样的错误。希望我的帖子能够对大家有所帮助。

我不知道如何找到解决方案,但经过几个小时的调试后,我注意到当我使用 Xcode 中内置的依赖项管理器使用 Firebase 初始化项目,然后尝试使用 CocoaPods 初始化 Firestore 时,就会出现问题。更具体地说,我做的第一件事是使用这个安装 Firebase:

Xcode 包依赖管理器

然后,我开始使用 CocoaPods,例如:

$ pod init
$ vim Podfile // adding 'firebase/firestore' dependency or specific line from https://github.com/firebase/firebase-ios-sdk
$ pod install
$ open <ProjectName>.xcworkspace
Run Code Online (Sandbox Code Playgroud)

在下一步中,我尝试检查基本的 Firestore 操作,例如使用数据创建集合和文档,当我运行应用程序时 - 我遇到了类似的错误。这是应用程序运行期间的异常。

我认为就我而言,有问题的事情可能是重复的,因为为项目提供了两次 Firebase(具有所有依赖项)。

因此,我删除了从 Xcode 管理器安装的依赖项。在我的中Info.plist 没有依赖项:

Xcode项目Info.plist

然后,我从终端删除了Pods目录并调用了更新。

$ rm -rf Pods
$ pod update
Run Code Online (Sandbox Code Playgroud)

所有依赖项均已重新安装,工作区已重新创建。现在,经过这些步骤后一切正常。