Error when trying to call setData(from: ) in the Cloud Firestore ios API. How can i fix it?

别等时光非礼了梦想. 提交于 2020-05-29 10:17:12

问题


Working in Swift..

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

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:

the line that is causing the build to fail:

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

    } catch let error {
      print(error)
    }

The Codable Struct:

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!


回答1:


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

Simple fix by adding this to your podfile

pod 'FirebaseFirestoreSwift'

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)

and this

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

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

and this line

.data(as: City.self)

and

.setData(from:

were the 'troublemakers' that needed the FirebaseFirestoreSwift



来源:https://stackoverflow.com/questions/61021668/error-when-trying-to-call-setdatafrom-in-the-cloud-firestore-ios-api-how-ca

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!