gpt4 book ai didi

ios - "cannot convert value of type"- Firebase Swift 中的错误

转载 作者:行者123 更新时间:2023-11-30 13:44:41 25 4
gpt4 key购买 nike

我正在尝试使用电子邮件和密码在 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)
}
})
}

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

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

由于以下行触发了上述错误,我当然无法构建项目:

                usersRef.setValue(users)

知道为什么会发生这种情况以及如何解决吗?

提前致谢。

最佳答案

找到了解决方案,并将其放在这里以供其他人将来引用:

@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)
}
})


}

问题是,在初始化 userAdditionalDetails 时,应包含字符串格式的导出变量。例如。

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

这有效,所有对象都在后端正确反射(reflect)。

关于ios - "cannot convert value of type"- Firebase Swift 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35115480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com