【问题标题】:Xcode Firebase | Cannot convert value of type'AuthDataResult' to expected argument type 'User'Xcode Firebase |无法将类型“AuthDataResult”的值转换为预期的参数类型“用户”
【发布时间】:2019-02-15 18:35:19
【问题描述】:

我尝试了所有可能的选项来编写它,但我似乎无法找出解决方案。

func signup(email: String, password: String) {
    Auth.auth().createUser(withEmail: emailText.text!, password: passwordText.text!, completion: { (user, error) in
        if error != nil {
            print(error!)
        }else {
            self.createProfile(user!) //Here's the problem described in the title //
            let homePVC = RootPageViewController()
            self.present(homePVC, animated: true, completion: nil)
        }
    })
}

func createProfile(_ user: User) {
    let newUser = ["email": user.email, "photo": "https://firebasestorage.googleapis.com/v0/b/ecoapp2.appspot.com/o/photos-1.jpg?alt=media&token=ee104f2d-ed9a-4913-8664-04fd53ead857"]
    self.databaseRef.child("profile").child(user.uid).updateChildValues(newUser) { (error, ref) in
        if error != nil {
            print(error!)
            return
        }
        print("Profile successfully created")
    }
}

【问题讨论】:

    标签: swift firebase firebase-authentication


    【解决方案1】:

    你需要

    Auth.auth().createUser(withEmail: email, password: password) { authResult, error in
      // ... 
       guard let user = authResult.user  else { reurn  }
       self.createProfile(user)
    }
    

    func createProfile(_ user: FIRUser ) { --- }
    

    【讨论】:

      【解决方案2】:

      从 Firebase API 5.0 开始,它的 createUser() 方法直接返回 FIRAuthDataResultCallback 而不是 User 对象。

      在您的情况下,您可以通过以下更改来修复它:

      func signup(email: String, password: String) {
              Auth.auth().createUser(withEmail: emailText.text!, password: passwordText.text!, completion: { (user, error) in
                  if error != nil {
                      print(error!)
                  }else {
                      self.createProfile(user!.user) //Here's how you can fix it 
                      let homePVC = RootPageViewController()
                      self.present(homePVC, animated: true, completion: nil)
                  }
              })
          }
      

      为了提高代码可读性,我将替换您的代码,如下所示:

      func signup(email: String, password: String) {
              Auth.auth().createUser(withEmail: emailText.text!, password: passwordText.text!, completion: { (authResult, error) in
                  if error != nil {
                      print(error!)
                  }else {
                      self.createProfile(authResult!.user)  
                      let homePVC = RootPageViewController()
                      self.present(homePVC, animated: true, completion: nil)
                  }
              })
          }
      

      【讨论】:

        猜你喜欢
        • 2019-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-27
        • 2016-07-02
        • 2016-03-21
        • 1970-01-01
        • 2018-01-07
        相关资源
        最近更新 更多