“无法转换类型的值” - Firebase Swift中的错误

时间:2021-10-02 20:01:12

I am trying to create a user in Firebase with an email & password and upon successful creation of the user record, I would go and update tree with additional attributes as child nodes of the user's UID. Here is what I have in code:

我正在尝试使用电子邮件和密码在Firebase中创建用户,并且在成功创建用户记录后,我将使用其他属性更新树作为用户UID的子节点。这是我在代码中的内容:

@IBAction func registerPressed(sender: AnyObject) {
        let connection = Firebase(url:"https://something.firebaseio.com")

        connection.createUser(self.emailTxtField.text, password: self.passwordTxtField.text,
            withValueCompletionBlock: { error, result in
                if error != nil {
                    // There was an error creating the account
                    print("Error Creating Account...Please try again")

                } else {
                    let uid = result["uid"] as? String
                    print("Successfully created user account with uid: \(uid)")

                    /*After creating the user, we want to update the tree with other attributes*/
                    let userAdditionalDetails = ["name": self.nameTxtField.text, "mobile": self.mobileTxtField.text, "username": self.usernameTxtField.text]
                    let usersRef = connection.childByAppendingPath("users")

                    let users = ["\(uid)": userAdditionalDetails]
                    usersRef.setValue(users)
                }
        })
    }

Although am not sure if the above will work or not (since this my first work with Firebase SDK), I am getting the following error:

虽然我不确定上述内容是否有效(因为这是我第一次使用Firebase SDK),但我收到以下错误:

cannot convert value of type “[String: [String : String?]]” to expected argument type "AnyObject!"

无法将“[String:[String:String?]]”类型的值转换为预期的参数类型“AnyObject!”

I am unable of course to build the project due to the above error being triggered at line:

由于在线触发上述错误,我当然无法构建项目:

                usersRef.setValue(users)

Any idea why this is happening and how to solve it?

知道为什么会这样,以及如何解决它?

Thanks in advance.

提前致谢。

2 个解决方案

#1


1  

Found the solution and just having it here for someone else's future reference:

找到解决方案,并将其放在此处供其他人未来参考:

@IBAction func registerPressed(sender: AnyObject) {
    let connection = Firebase(url:"https://something.firebaseio.com")

    connection.createUser(self.emailTxtField.text, password: self.passwordTxtField.text,
        withValueCompletionBlock: { error, result in
            if error != nil {
                // There was an error creating the account
                print("Error Creating Account...Please try again")

            } else {
                let uid = result["uid"] as? String
                print("Successfully created user account with uid: \(uid)")

                /*After creating the user, we want to update the tree with other attributes*/
                let userAdditionalDetails = ["name": "\(self.nameTxtField.text!)", "mobile": "\(self.mobileTxtField.text!)", "username": "\(self.usernameTxtField.text!)"]
                let usersRef = connection.childByAppendingPath("users")

                let users = ["\(uid)": userAdditionalDetails]
                usersRef.setValue(users)
            }
    })


}

The catch is that when initializing userAdditionalDetails, should include the outlets variables in string format. E.g.

问题是,在初始化userAdditionalDetails时,应该以字符串格式包含outlet变量。例如。

"username": "\(username.text!)"

This worked and all objects reflected correctly in backend.

这有效,所有对象在后端都能正确反映。

#2


-1  

In the call to usersRef.setValue(users), the error message states that the parameter is expected to be of type AnyObject!. Therefore, you must cast users to be of type AnyObject!:

在对usersRef.setValue(users)的调用中,错误消息指出该参数应该是AnyObject!类型。因此,您必须将用户强制转换为AnyObject!:

let users = ["\(uid)": userAdditionalDetails]
usersRef.setValue(users as? AnyObject)

#1


1  

Found the solution and just having it here for someone else's future reference:

找到解决方案,并将其放在此处供其他人未来参考:

@IBAction func registerPressed(sender: AnyObject) {
    let connection = Firebase(url:"https://something.firebaseio.com")

    connection.createUser(self.emailTxtField.text, password: self.passwordTxtField.text,
        withValueCompletionBlock: { error, result in
            if error != nil {
                // There was an error creating the account
                print("Error Creating Account...Please try again")

            } else {
                let uid = result["uid"] as? String
                print("Successfully created user account with uid: \(uid)")

                /*After creating the user, we want to update the tree with other attributes*/
                let userAdditionalDetails = ["name": "\(self.nameTxtField.text!)", "mobile": "\(self.mobileTxtField.text!)", "username": "\(self.usernameTxtField.text!)"]
                let usersRef = connection.childByAppendingPath("users")

                let users = ["\(uid)": userAdditionalDetails]
                usersRef.setValue(users)
            }
    })


}

The catch is that when initializing userAdditionalDetails, should include the outlets variables in string format. E.g.

问题是,在初始化userAdditionalDetails时,应该以字符串格式包含outlet变量。例如。

"username": "\(username.text!)"

This worked and all objects reflected correctly in backend.

这有效,所有对象在后端都能正确反映。

#2


-1  

In the call to usersRef.setValue(users), the error message states that the parameter is expected to be of type AnyObject!. Therefore, you must cast users to be of type AnyObject!:

在对usersRef.setValue(users)的调用中,错误消息指出该参数应该是AnyObject!类型。因此,您必须将用户强制转换为AnyObject!:

let users = ["\(uid)": userAdditionalDetails]
usersRef.setValue(users as? AnyObject)