尝试在 Cloud Firestore ios API 中调用 setData(from:) 时出错.我该如何解决? [英] Error when trying to call setData(from: ) in the Cloud Firestore ios API. How can i fix it?

查看:18
本文介绍了尝试在 Cloud Firestore ios API 中调用 setData(from:) 时出错.我该如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Swift 中工作..

Working in Swift..

我正在尝试调用 .setData(from: ) 将 Codable 结构写入云 Firestore 数据库中的文档,如 Firebase 文档中所述:

I'm trying to call .setData(from: ) to write a Codable struct to a document in the cloud firestore database as outlined in the Firebase docs here to:

https://firebase.google.com/docs/firestore/manage-data/add-data#custom_objects

但是,我收到错误消息:参数标签 '(from:)' 与任何可用的重载不匹配"

However, I'm getting the error: "Argument labels '(from:)' do not match any available overloads"

奇怪的是,我能够在模拟器上构建并运行一次,并成功发布文档,但现在显然编译器正在调用此错误并导致构建失败.相关代码如下:

The odd thing is I was able to build and run on the simulator once, and successfully posted a document, but now obviously the compiler is calling this error and causing the build to fail. relevant code below:

导致构建失败的行:

do {
      try collectionRef.document(lensSet.id.uuidString).setData(from: lensSet)

    } catch let error {
      print(error)
    }

可编码结构:

struct LensSet: Codable, Identifiable {
  var id: UUID

  // Coding KEYS
  private enum CodingKeys: String, CodingKey {
      case id
  }
}

他们在文档中所说的调用函数似乎不存在?任何帮助表示赞赏!

It seems like the function they are saying to call in the docs maybe doesn't exist? Any help appreciated!

推荐答案

文档并没有完全错误,只是其中一个部分不完整.

The documentation isn't exactly wrong, it's just incomplete in one section.

对于 TL;DR

请参阅 Peter Friese 的指南:在 Swift 中映射 Firestore 数据

See the guide from Peter Friese: Mapping Firestore Data in Swift

详细解释

Firebase 添加了一个扩展以简化自定义对象的制作,但并未在文档中提及该扩展.(我假设它是一个扩展)

Firebase added an extension to make crafting custom obects easier but didn't mention that extension in the documentation. (I assume its an extension)

自定义对象的文档是需要扩展的代码示例

The documentation on Custom Objects is an example of the code that needs the extension

通过将其添加到您的 podfile 来简单修复

Simple fix by adding this to your podfile

pod 'FirebaseFirestoreSwift'

然后在终端或 cocoapods 中进行 pod 更新.

and then do a pod update either in terminal or cocoapods.

然后在您的类中只需更新导入以包含

Then in your Class just update the imports to include that

import Cocoa
import FirebaseCore
import FirebaseFirestore
import FirebaseFirestoreSwift

参考文档中的代码是这样的

For reference the code in the docs was this

try db.collection("cities").document("LA").setData(from: city)

还有这个

let docRef = db.collection("cities").document("LA")

 docRef.getDocument { (document, error) in
    let result = Result {
    try document.flatMap {
       try $0.data(as: City.self)
    }
 }

还有这一行

.data(as: City.self)

.setData(from:

是需要 FirebaseFirestoreSwift 的麻烦制造者"

were the 'troublemakers' that needed the FirebaseFirestoreSwift

这篇关于尝试在 Cloud Firestore ios API 中调用 setData(from:) 时出错.我该如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆