Firestore:如何在IOS(Swift)中从Firestore获取对象数据并在tableview中显示
我在firestore数据库中有一个类似对象形式的结构。我需要从firestore检索数据,并在swift的TableView中显示它Firestore:如何在IOS(Swift)中从Firestore获取对象数据并在tableview中显示,ios,swift,firebase,google-cloud-firestore,Ios,Swift,Firebase,Google Cloud Firestore,我在firestore数据库中有一个类似对象形式的结构。我需要从firestore检索数据,并在swift的TableView中显示它 - collection//users -authid - collection//Split - auth id - collection//SentInvitations -automatic id -Invitee(i)(object)
-
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=受邀人数
现在我需要检索这些所有被邀请者并显示到表视图中
代码我试图从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")
}
}
}
和我的模型课邀请
当我执行此代码时,它对else条件不存在的文档执行了修改,但在firestore中它包含该文档。要得到这些,还有其他方法可以解决吗
好的,在与Vijju讨论问题之后,我们忽略了一点,即Firestore在其字典中不使用
Swift
类型,而是使用Objective-C
类型。因此,应首先将预期为Int
的内容转换为NSNumber
,然后从该NSNumber
获得其Int
值。因此,用代码总结:
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)
}
您能否提供firestore数据库控制台的屏幕截图,以便我们可以从那里看到结构。提供结构并不能帮助我们查明您在工作期间可能犯的错误fetching@Serj我已经添加了我的数据结构的屏幕截图在应用flatmap之后,您的Invite dictionary对象是否使用Firestore中的键和值初始化?我没有看到城市变量。@ericl我在document.flatmap行初始化:如果let city=document.flatmap({$0.data().flatmap({(数据)在return Invite(字典:data)中)您的结构“Invite”仅用“金额”和“电话号码”初始化。如果您在“城市”设置断点并进行检查,该值是多少?是的,我没有找到。谢谢您的支持!如果我的答案是正确的,并且有助于别忘了向上投票,并将其标记为其他读者的正确答案,则不必客气!@vijju我会用你的截图更新我的答案,为什么我怀疑它的作者在拆分后是一个作者id,我确信这一点
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)
}