swift - 想要从 firebase 获取用户数据到字典中

标签 swift dictionary firebase firebase-realtime-database

我希望你能帮助我: 我尝试将用户数据从 firebase 数据库获取到用户类中并调用“setValuesForKeys”。我确实知道我的类属性必须与 firebase 字典中的完全相同,但我收到错误“此类与关键城市的键值编码不兼容。”

func fetchUsers(){
    Database.database().reference().child("users").observe(.childAdded, with: { (snapshot) in
        if let usersDictionary = snapshot.value as? [String: String] {
            let users = Userdata()
            users.setValuesForKeys(usersDictionary)
        }
    }, withCancel: nil)
}

我的用户类别是

class Userdata: NSObject {
    var email: String?
    var password: String?
    var firstname: String?
    var lastname: String?
    var street: String?
    var streetno: String?
    var zipcode: String?
    var city: String?
    var phone: String?  }

来自 firebase 的快照看起来像

Snap (ndLBXXX75Oe9Y1PXrqfISL8A4v82) {
city = Washington;
email = "1@a.com";
firstname = Andre;
lastname = Doe;
password = xxxxxx;
phone = "";
street = "Mainstreet";
streetno = 1;
zipcode = 11111;

}

数据库中的字典看起来像

["city": Washington, "firstname": Andre, "lastname": Doe, "email": 1@a.com, "password": xxxxxx, "streetno": 1, "phone": , "street": Mainstreet, "zipcode": 11111]

到目前为止我有一个解决方案:

users.city = dictionary["city"]

我的问题:我确实想了解错误消息“此类不符合关键城市的键值编码”背后的问题。因为类中的键和 firebase 快照中的键看起来是相同的。

最佳答案

工作解决方案:我必须扩展我的用户类别。现在,整个用户类看起来像:

这是代码:

 import Foundation

    class UserData: NSObject {
        var id: String?
        var email: String?
        var password: String?
        var salutation: String?
        var degree: String?
        var firstname: String?
        var lastname: String?
        var street: String?
        var streetno: String?
        var zipcode: String?
        var city: String?
        var phone: String?
        var profileImage: String?

    init(dictionary: [String: Any]) {
        self.city = dictionary["city"] as? String
        self.id = dictionary["id"] as? String
        self.email = dictionary["email"] as? String
        self.salutation = dictionary["salutation"] as? String
        self.degree = dictionary["degree"] as? String
        self.firstname = dictionary["firstname"] as? String
        self.lastname = dictionary["lastname"] as? String
        self.password = dictionary["password"] as? String
        self.phone = dictionary["phone"] as? String
        self.street = dictionary["street"] as? String
        self.streetno = dictionary["streetno"] as? String
        self.zipcode = dictionary["zipcode"] as? String
        self.profileImage = dictionary["profileImage"] as? String

    }
}

关于swift - 想要从 firebase 获取用户数据到字典中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45715925/

相关文章:

node.js - 错误 : The default Firebase app already exists. 真的吗?

快速表格 View ,在表格 View 单元格中播放视频,内存泄漏

ios - 使用observedProgress时如何将UIProgressView重置为0

ios - 滚动 tableview 导致复选标记消失 |处理 Swift 5 中的可重用单元格

swift - 如何使用 Swift 以编程方式在 iPhone 上打开“朗读屏幕”?

c# - WPF 绑定(bind)到带有类的字典

python - 索引计数字典的嵌套列表

ios - SWIFT 3 - CGImage 副本始终为零

python - 在 python 中的字典中移动引用的文件

android - 如何从 Firebase 中仅获取新添加的数据?