OGeek|极客世界-中国程序员成长平台

标题: ios - 试图快速从firebase数据库中获取用户 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:03
标题: ios - 试图快速从firebase数据库中获取用户

尝试快速从 firebase 数据库中获取用户,但我收到错误 此类与键的键值编码不兼容。我不认为我做错了什么,因为它不是我的第一个应用程序。我能够以完全相同的方式在其他应用程序中获取用户。但它现在不工作。所有必要的细节如下。请帮助,谢谢 这是代码

func fetchUsers() {
    Database.database().reference().child("Users").observe(.childAdded, with: { (snapshot) in
        if let dictionary = snapshot.value as? [String: AnyObject] {
            print(snapshot)
            let user = Users()
            user.setValuesForKeys(dictionary)
            //print(user.Email ?? "")
            //print(user.Name ?? "")

        }
    }, withCancel: nil)
}

这是模型:

import UIKit

class Users: NSObject {
    var Name : String?
    var Email : String?
}

数据库截图 enter image description here

控制台出错

ChatHub[2089:904743] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Email.' * First throw call stack: (0x182dd7d38 0x1822ec528 0x182dd7a00 0x1836e3780 0x18377fc0c 0x1048000d8 0x104800174 0x1048ac760 0x1048d7e40 0x105b0d49c 0x105b0d45c 0x105b12050 0x182d7ff20 0x182d7dafc 0x182c9e2d8 0x184b2ff84 0x18c24b880 0x104806a64 0x1827c256c) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)



Best Answer-推荐答案


在我看来,您不应该在开头使用大写字母命名您的实例/对象/变量,请参阅:https://softwareengineering.stackexchange.com/questions/202031/what-is-the-reason-for-using-lowercase-for-the-first-word-in-a-local-variable-e .

回到您的问题,您是否尝试过重命名 Users 的属性?尽量使用Struct,不需要像这样使用NSObject的类和子类,方便初始化。我怀疑您的 Email 变量名称是导致错误的原因。

struct Users {
    var name: String?
    var email: String?
}

Users 的新实例:

let user = Users(name: "name here", email: "email here")

我希望这会有所帮助!

关于ios - 试图快速从firebase数据库中获取用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46877135/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4