如何解决难以将数据传递到UI-Swift
早上好,我正在尝试将数据传递到“个人资料用户界面”视图。
这是我的客户类别:
prop_id prop_agent prop_title prop_slug prop_mandate id prop_gallery_id prop_gallery
37 2 House in switzerland house-in-switzerland 1 4 6 main1.png
37 2 House in switzerland house-in-switzerland 1 4 6 main2.png
37 2 House in switzerland house-in-switzerland 1 4 6 main3.png
这是我的代码,通过该代码,我尝试解析Firestore中的数据以显示客户详细信息:
class Customer {
// Creating a customer
let name: String
let surname: String
let contactNo: String
let email: String
init(name: String,surname: String,contactNo: String,email: String) {
self.name = name
self.surname = surname
self.contactNo = contactNo
self.email = email
}
}
Firestore方面的所有功能都可以正常工作,并且我能够读取/检索数据,但是我正努力通过我的客户类将数据传递给UI。使用打印语句似乎customer对象为nil。
解决方法
func getDataFromFirebase(completion:@escaping() -> ()){
let userID = Auth.auth().currentUser?.uid
// Locate the user information on Firestore
customerRefCollection.document(userID!).getDocument { (snapshot,error) in
if let err = error {
debugPrint("Error fetching documents: \(err)")
}
else {
// Ensure that if there's nothing in the document that the function returns
guard let snap = snapshot else {return}
// Parse the data to the customer model
let data = snap.data()
let name = data?["name"] as? String ?? ""
let surname = data?["surname"] as? String ?? ""
let email = data?["email"] as? String ?? ""
let contact = data?["contact no"] as? String ?? ""
// Create the customer and pass it to the global variable
let cus = Customer(name: name,surname: surname,contactNo: contact,email: email)
self.customer = cus
}
completion()
}
}
getDataFromFirebase{
customerRefCollection = Firestore.firestore().collection("customers")
nameLabel.text = customer?.name
surnameLabel.text = customer?.surname
emailLabel.text = customer?.email
contactLabel.text = customer?.contactNo
}
因此,您在这里基本上要做的是首先从firebase中获取数据,并且只有在完成这项工作之后,因此,您将在完成块中将数据设置为视图。您只需在viewDidLoad中调用该方法即可。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。