Why I getting an error message in the line that says:
usersReference.updateChildValues(values, withCompletionBlock: { (err, ref)
The error message says:
Expression implicitly coerced from '[String : String?]' to '[AnyHashable : Any]'
What changes could I make to my code to prevent the error message?
Here is all of my code from the view controller:
import UIKit
import Firebase
import FirebaseAuth
class RegisterViewController: UIViewController {
private var ref: DatabaseReference! // референс к БД
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var passwordField: UITextField!
@IBOutlet weak var firstNameField: UITextField!
@IBOutlet weak var lastNameField: UITextField!
@IBOutlet weak var cityField: UITextField!
@IBOutlet weak var telNumField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference() // инициализация БД
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func nextButtonPressed(_ sender: Any) {
Auth.auth().createUser(withEmail: self.emailField.text!, password: self.passwordField.text!) { (user, error) in
if error != nil {
print(error!)
self.showAlert(title: "Error!", msg: "Invalid information", actions: nil)
return
}
print("Registration succesfull")
guard let uid = user?.uid else { //доступ к ID пользователя
return
}
self.ref = Database.database().reference() // инициализация БД
let usersReference = self.ref.child("users").child(uid)
let values = ["firstname": self.firstNameField.text, "lastname": self.lastNameField.text, "email": self.emailField.text, "city": self.cityField.text, "telnumber": self.telNumField.text]
usersReference.updateChildValues(values, withCompletionBlock: { (err, ref) in
if let err = err {
print(err)
return
}
print("Saved user successfully into Firebase db")
})
}
}
this isn't error where compiler won't let you run your code without fixing it, this is just warning. But message of this warning is important since if you didn't fix it, your string would be saved like this:
So, in your case problem is, that you're passing optional property
text
of typeString?
asAny
which shouldn't be optional.You can silence this warning and fix your code by force-unwrapping
text
properties of your text fields (it's safe to force-unwrap it here because, regarding to docs, this property is nevernil
)