ios - Swift Firestore 获取文档 ID

标签 ios swift firebase firebase-realtime-database google-cloud-firestore

我正在为我的数据库使用 FirebaseFirestore。

我有一组带有名称和描述的列表。我还想获取每个列表的唯一 documentId。这可能吗?

列表.swift

struct List {
    var name:String
    var description:String

    var dictionary:[String:Any] {
        return [
            "name":name,
            "description":description
        ]
    }
}

ListTableViewController.swift

func getLists() {
    if Auth.auth().currentUser != nil {
        db.collection("lists").getDocuments { (querySnapshot, error) in
            if let error = error {
                print("Error getting documents: \(error)")
            } else {
                self.listArray = querySnapshot!.documents.flatMap({List(dictionary: $0.data())})
                DispatchQueue.main.async {
                    self.tableView.reloadData()
                }
            }
        }
    }
}

我发现您可以通过...获取 documentId

    for document in querySnapshot!.documents {
        print("\(document.documentID) => \(document.data())")
    }

但是我如何将其存储在我的列表中以便稍后调用?

最佳答案

1.假设您正在获取快照中的文档。

    guard let documents = ducomentSnapshot?.documents else {
      print("Error fetching documents: \(error!)")
      return
   }

//使用此代码获取每个文档对象的 documentId。

  for i in 0 ..< documents.count {
        let dictData = documents[i].data()
        let documentID = documents[i].documentID
        print("Document ID \(documentID)")
    }

关于ios - Swift Firestore 获取文档 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47743458/

相关文章:

ios - 以编程方式推送、继续、召唤、导航至 View SwiftUI

ios - Swift - 导航栏未出现在自定义单元格的 Segue 中

ios - 如何使用 swift 从 [Any] 访问 key

ios - 无法更改 UICollectionview 中的 View 颜色

swift - 获取一天中的所有时间(以日期格式)从上午 9 点到晚上 11 点,并保存在 Swift 的数组中

ios - 未能绑定(bind) EAGLDrawable iOS swift

ios - iOS 中 G2、D♯3、D3 的 hexa MIDI 消息是什么?

javascript - 我不知道如何在 case admin.firestore 中设置 firebase firestore.settings?

c# - Unable install Xamarin Firebase - Common 32.961.0 to xamarin.forms解决方法

swift - 在TableView Swift 4中显示Firebase数据