Swift和Firebase无法分配类型为'AuthDataResult?'的值输入“用户?

时间:2019-04-10 21:48:17

标签: swift firebase firebase-authentication

我正在重新访问旧的应用程序,但是在使用新功能之前,我想修复所有错误。我不确定这是由较新的Swift语法还是对Firebase(或两者)的更新引起的,但是以前有效的代码现在引起2个错误。

var user = Auth.auth().currentUser


Auth.auth().createUser(withEmail: theEmail, password: thePassword, completion: { (user, error) in
  if let theError = error {

    var errMessage = "An unknown error occured."
    if let errCode = AuthErrorCode(rawValue: (theError._code)) {
      switch errCode {

      case .invalidEmail:
        errMessage = "The entered email does not meet requirements."
      case .emailAlreadyInUse:
        errMessage = "The entered email has already been registered."
      case .weakPassword:
        errMessage = "The entered password does not meet minimum requirements."
      default:
        errMessage = "Please try again."
      }
    }
    completion(nil, errMessage)
  } else {
    self.user = user
    completion(user, nil)
  }
})

self.user = user上,错误消息为Cannot assign value of type 'AuthDataResult?' to type 'User?'

completion(user, nil)上,错误消息为Cannot convert value of type 'AuthDataResult?' to expected argument type 'User?'

有人可以解释一下曾经有效的代码为何不再起作用,以及我如何才能使它达到当前的期望

1 个答案:

答案 0 :(得分:2)

替换

self.user = user

使用

Auth.auth().createUser(withEmail: theEmail, password: thePassword, completion: { (authData, error) in

    .....

    if let res = authData?.user {
      self.user = res
    }

}