雨燕3.1 - .setValuesForKeys和火力地堡未能以检索用户(Swift 3.1

2019-09-27 10:51发布

所以,我有这一块是想从数据库中提取用户,并将其存储在一个阵列(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'

有任何想法吗?

Answer 1:

错误unrecognized selector sent to instance意味着你试图设置一个关键错误类型的值。 这不是从你的代码清楚什么样的对象User的,或者是在什么密钥dictionary对象,但其中一个值是不是该类型User期待该键。

为了解决这个问题,你需要在字典中投每个值正确的类型。



Answer 2:

... 我修好了它。

更换

user.setValuesForKeys(dictionary)

user.name = dictionary["name"] as? String
user.email = dictionary["email"] as? String
user.picture = dictionary["picture"] as? String
user.facebookID = dictionary["facebookID"] as? String
user.status = dictionary["status"] as? String
user.date_joined = dictionary["date_joined"] as? String
user.last_login = dictionary["last_login"] as? String
user.Longitude = dictionary["Longitude"] as? String
user.Latitude = dictionary["Latitude"] as? String


文章来源: Swift 3.1 - .setValuesForKeys and Firebase failing to retrive users