无法将“[String : String?]”类型的值转换为预期的参数类型“String”
Posted
技术标签:
【中文标题】无法将“[String : String?]”类型的值转换为预期的参数类型“String”【英文标题】:Cannot convert value of type '[String : String?]' to expected argument type 'String' 【发布时间】:2021-09-08 15:22:27 【问题描述】:我正在尝试将一些用户数据添加到我的 firebase 实时数据库中,但是当我尝试使用数据添加子节点时,我在下面收到此错误。我尝试将userData
变量的类型更改为String
,但没有任何改变。
错误:
Cannot convert value of type '[String : String?]' to expected argument type 'String'
我的代码:
private func firebaseAuthentication(withUser user: GIDGoogleUser)
if let authentication = user.authentication
let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken, accessToken: authentication.accessToken)
Auth.auth().signIn(with: credential) (_, error) in
if let error = error
print(error.localizedDescription)
else
// Add user data to real-time db if user does not already exists
let userData = [
"id" : Auth.auth().currentUser?.uid,
"email" : Auth.auth().currentUser?.email,
"name" : Auth.auth().currentUser?.displayName,
"photoUrl": "none",
"premium": "false"
]
ref.observeSingleEvent(of: .value) snapshot in
if !snapshot.hasChild(Auth.auth().currentUser?.uid ?? "noid")
// user does not exist
usersRef.child(Auth.auth().currentUser?.uid ?? "noid").child(userData) // here i'm getting the error
withCancel: error in
self.state = .signedIn
【问题讨论】:
【参考方案1】:替换
.child(userData)
与
.setValue(userData)
【讨论】:
以上是关于无法将“[String : String?]”类型的值转换为预期的参数类型“String”的主要内容,如果未能解决你的问题,请参考以下文章