我希望你能帮助我:我尝试将firebase数据库中的userdata提取到用户类并调用“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快照中的键看起来是一样的 .
1 回答
工作解决方案:我不得不扩展我的用户类 . 现在,整个用户类看起来像:
这是代码: