我一直在尝试将从 Firebase 的 Cloud Firestore 检索到的文档转换为 Swift 5 中的自定义对象。我正在关注文档:
但是,Xcode 显示错误
Value of type 'NSObject' has no member 'data'
对于线路 try $0.data(as: JStoreUser.self)
.我已将结构定义为 Codable
.编码:
func getJStoreUserFromDB() {
db = Firestore.firestore()
let user = Auth.auth().currentUser
db.collection("users").document((user?.email)!).getDocument() {
(document, error) in
let result = Result {
try document.flatMap {
try $0.data(as: JStoreUser.self)
}
}
}
}
用户结构:public struct JStoreUser: Codable {
let fullName: String
let whatsApp: Bool
let phoneNumber: String
let email: String
let creationDate: Date?
}
截图:有谁知道如何解决这个问题?
最佳答案
联系 firebase 团队后,我找到了我正在寻找的解决方案。原来我必须做import FirebaseFirestoreSwift
明确地而不是仅仅做 import Firebase
.此后错误将消失。 (当然,您需要先将 pod 添加到您的 podfile 中:D)
关于ios - 如何在 Swift 5 中将文档转换为自定义对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59548819/