【问题标题】:Cannot convert value of type '[String : String?]' to expected argument type 'String'无法将“[String : String?]”类型的值转换为预期的参数类型“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
      }
    }
  }
}

【问题讨论】:

    标签: ios swift firebase


    【解决方案1】:

    替换

    .child(userData)
    

    与

    .setValue(userData)
    

    【讨论】:

      猜你喜欢
      • 2016-01-14
      • 1970-01-01
      • 2020-12-30
      • 1970-01-01
      • 1970-01-01
      • 2021-03-13
      • 2015-12-24
      • 1970-01-01
      • 2023-04-05
      相关资源
      最近更新 更多