0
votes

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:

@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:

cannot convert value of type “[String: [String : String?]]” to expected argument type "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

2 Answers

1
votes

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.

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

This worked and all objects reflected correctly in backend.

-1
votes

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!:

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