小编Tia*_*hen的帖子

如何在 Swift 5 中将文档转换为自定义对象?

我一直在尝试将从 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)
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

用户结构:

public struct JStoreUser: Codable {
    let fullName: String
    let whatsApp: Bool
    let phoneNumber: String
    let email: String
    let creationDate: Date?
} …
Run Code Online (Sandbox Code Playgroud)

ios firebase google-cloud-platform swift google-cloud-firestore

9
推荐指数
1
解决办法
5509
查看次数