Swift 3.1-.setValuesForKeys和Firebase无法检索用户 [英] Swift 3.1 - .setValuesForKeys and Firebase failing to retrive users

查看:104
本文介绍了Swift 3.1-.setValuesForKeys和Firebase无法检索用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我假设有这段代码可以从数据库中获取用户并将其存储在Array(drivingUsers)中.我没有收到任何错误,但是当我运行该应用程序时,它崩溃了.

So I have this piece of code that is suppose to fetch users from the database and store them in an Array (drivingUsers). I get no errors but when I run the app it crashes.

 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期望该键的类型.

The error unrecognized selector sent to instance means you tried to set a key to a value of the wrong type. It's not clear from your code what kind of object User is, or what keys are in the dictionary object, but one of the values is not the type that User is expecting for that key.

要修复此问题,您需要将字典中的每个值都强制转换为正确的类型.

To fix it, you'll need to cast each value in the dictionary to the right type.

这篇关于Swift 3.1-.setValuesForKeys和Firebase无法检索用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆