所以我有这段代码可以从数据库中获取用户并将它们存储在一个数组 (drivingUsers) 中。我没有收到任何错误,但是当我运行该应用程序时它崩溃了。
func fetchUser() {
FIRDatabase.database().reference().child("user_profiles").observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let user = User()
user.setValuesForKeys(dictionary) //Error
self.drivingUsers.append(user)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}, withCancel: nil)
}
错误日志:
2017-04-29 00:41:46.842568 VEXI[1745:369755] -[__NSCFNumber length]: unrecognized selector sent to instance 0x174a34500
2017-04-29 00:41:46.844084 VEXI[1745:369755] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x174a34500'
有什么想法吗?
最佳答案
错误 unrecognized selector sent to instance
表示您尝试将键设置为错误类型的值。从您的代码中不清楚 User
是什么类型的对象,或者 dictionary
对象中有哪些键,但其中一个值不是 User 的类型
需要那个键。
要修复它,您需要将字典中的每个值转换为正确的类型。
关于Swift 3.1 - .set Values For Keys 和 Firebase 无法检索用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43689839/