ios - 火存储 : How to get the object data from firestore in IOS (Swift) and display it in tableview

标签 ios swift firebase google-cloud-firestore

我在 firestore 数据库中有一个类似于对象形式的结构。我需要从 firestore 检索数据并将其显示在 swift 的 tablebview 中

-

collection//users
        -authid
       - collection//Split
         - auth id
         - collection//SentInvitations
           -automatic id
             -Invitee(i)(object)
                .name:bnnn
                .phonenumber:567788
                .amount:123
            - Invitee(2)
                .name:aaaa
                .phonenumber:987654321
                .amount:198

其中 i = 受邀者人数 现在我需要检索所有受邀者并显示到表格 View 中

我尝试从 firestore 检索数据的代码

func loadData(){
    let authid = Auth.auth().currentUser?.uid
   let docRef =  db.collection("deyaPayUsers").document(authid!).collection("Split").document(authid!).collection("SentInvitations").document(senderautoid!)
        docRef.getDocument { (document, error) in
                        if let city = document.flatMap({
                            $0.data().flatMap({ (data) in
                                return Invite(dictionary: data)
                            })
                        }) {
                            print("City: \(city)")
                        } else {
                            print("Document does not exist")
                        }
                    }

    }

和我的模型类 Invite

import Foundation
import FirebaseFirestore

protocol DocumentSerializable2
{
    init?(dictionary:[String:Any])
}
struct Invite {
    var PhoneNumber:Int
    var Amount:Int
    //var Status:String
    var dictionary:[String:Any]{
        return [
            "PhoneNumber":PhoneNumber,
            "Amount":Amount,
            ]
    }
}
    extension  Invite : DocumentSerializable2{
    init?(dictionary: [String:Any]){
        guard let amount = dictionary["Amount"] as? Int,
            let phonenumber = dictionary["PhoneNumber"] as? Int else { return nil}
        self.init(PhoneNumber:phonenumber, Amount:amount)

        }
    }

当我执行此代码时,它会执行 else 条件文档不存在,但在 firestore 中它包含该文档。要获得这些,还有其他方法可以解决 enter image description here

enter image description here

最佳答案

好吧,在与 Vijju 讨论问题之后,我们忽略了一点,即 Firestore 在其字典中不使用 Swift 类型,而是使用 Objective-C 类型。因此,应该首先将预期为 Int 的内容转换为 NSNumber,然后从该 NSNumber 中获取其 Int > 值(value)。所以用代码总结一下:

init?(dictionary: [String:Any]){
        guard let nsNumberAmount = dictionary["Amount"] as? NSNumber,
            let nsNumberPhoneNumber = dictionary["PhoneNumber"] as? NSNumber else { return nil}
        let phoneNumber = nsNumberPhoneNumber.intValue
        let amount = nsNumberAmount.intValue

        self.init(PhoneNumber:phonenumber, Amount:amount)
    }

关于ios - 火存储 : How to get the object data from firestore in IOS (Swift) and display it in tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50653600/

相关文章:

ios - 按 UserDefaults 排序保存和检索数组时出现问题

swift - TwitterKit 登录总是第一次尝试失败然后第二次尝试成功(Swift、TwitterKit、Firebase)

android - 如何修复 recyclerview 适配器的这种奇怪行为?

objective-c - 多对多谓词

ios - IOS 7中的电子邮件地址验证

swift - 交换栏按钮项目

json - 想查看json元数据

ios - 如何更改导航栏标题的颜色?

ios - Cocoapods:关闭 MagicalRecord 注销

php - Firebase PHP CURL 身份验证