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

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

问题描述

在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!

推荐答案

文档并非完全错误,只是一部分内容不完整。 Firebase添加了一个扩展,使制作自定义对象更容易,但在文档中未提及该扩展。 (我假设它是扩展名)

The documentation isn't exactly wrong, it's just incomplete in one section. 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'

,然后在终端机或coapapods中进行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

文档中的代码仅供参考

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天全站免登陆