gpt4 book ai didi

快速错误 - 线程 1 : signal SIGABRT for setValuesForKeys

转载 作者:行者123 更新时间:2023-11-30 11:53:58 25 4
gpt4 key购买 nike

func fetchUser() {
Database.database().reference().child("users").observe(.childAdded, with: { (DataSnapshot) in
if let dictionary = DataSnapshot.value as? [String: AnyObject] {
let user = User()
user.setValuesForKeys(dictionary.)
print(user.name!, user.email!)

}
}, withCancel: nil)

}

将 Firebase 数据库中的数据添加到我的应用程序时,将字典中的信息添加到我的用户数组(我为其创建了一个对象)时,它会引发 SIGABRT 错误。我正在做的事情有什么问题吗?

日志中的错误消息是这样的:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<ChatApp.User 0x10bdeec70> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key name.'
*** First throw call stack:
(0x18136a364 0x1805b0528 0x18136a02c 0x181c82630 0x181d1e7dc 0x100016170 0x10001645c 0x10016dd7c 0x100199524 0x1012a92cc 0x1012a928c 0x1012adea0 0x181312544 0x181310120 0x18122fe58 0x1830dcf84 0x18a8af67c 0x100025fbc 0x180d4c56c)
libc++abi.dylib: terminating with uncaught exception of type NSException

最佳答案

当数据库中的键与用户对象类中的变量名称不匹配时,就会发生这种情况。您可以通过自己手动执行来避免这种情况,如下所示:

    let reference = Database.database().reference()
reference.child("users").observe(.childAdded, with: { (snapshot) in

guard let dictionary = snapshot.value as? [String: AnyObject] else { return }
guard let username = dictionary["username"] as? String else { return }

let user = User()
user.username = username
// do this for everything the user object needs to hold.

}, withCancel: nil)

关于快速错误 - 线程 1 : signal SIGABRT for setValuesForKeys,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48035675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com