【问题标题】:Cannot convert value type "User?" to expected argument type "User!"无法转换值类型“用户?”到预期的参数类型“用户!”
【发布时间】:2018-01-07 20:19:38
【问题描述】:

我刚刚为 Firebase 升级了我的 Xcode 项目中的 pod 文件,它显示了一堆错误。注册用户时出现错误Cannot convert value type "User?" to expected argument type "User!"

我不确定是什么问题。我尝试更改一些可选值以使其正常工作,但我似乎不太了解问题所在。

这是我有错误的函数:

代码已修改为我的问题的解决方案

import Firebase
// Sign Up Function
func signUp(username: String, name: String, email: String, password: String){

    Auth.auth().createUser(withEmail: email, password: password, completion: { (user, error) in
        if error == nil {
            self.setUserInfo(user: user, username: username, name: name, email: email, password: password)

        }
        else{
            print(error?.localizedDescription as Any)
        }
    })

}

// Set the user info to Firebase storage. username, and password
private func setUserInfo(user: Firebase.User!, username: String, name: String, email: String, password: String){

       if error == nil {
           self.saveUserInfo(user: user, username: username, name: name, password: password)
       }
       else{
           print(error?.localizedDescription as Any)
       }
}

编辑: 正如用户 ryanwils 所说,无论我在哪里使用 FIRUser,都需要替换为 Firebase.User

这里有一个指向迁移指南的链接,进一步解释了这一点:https://firebase.google.com/docs/reference/ios/naming-migration-guide

请注意,代码已更新,因此截至本次编辑当天没有出现错误

【问题讨论】:

  • 试试这个:self.setUserInfo(user: user!, username: username, name: name, email: email, password: password)
  • @Bruno Recillas 您的用户对象不是可选的。尝试更改为用户?
  • @OMK 我之前试过了,还是一样的错误/:
  • @TusharSharma 当我这样做时,它告诉我Optional chain has no effect, expression already produces "User?"
  • 如果你不提供任何类型只是给用户。

标签: ios swift firebase swift3 firebase-authentication


【解决方案1】:

新的 Firebase 版本在大多数类型上删除了 FIR 前缀,因此现在 FIRUser 被称为 User。您的应用程序中是否已有 User 结构或类?如果是这样,您需要更改setUserInfo 调用中的参数应该接受Firebase.User(注意没有!),而不是User!

然后,当您调用它时,您可以使用if letguard let 抓取它,或者在传入时使用user! 强制解包。

如果不是这样,请告诉我,我可以尝试进一步提供帮助!

编辑:包括指向 Firebase 4 Swift Migration Guide 的链接,该链接解释了所有必要的更改。

【讨论】:

【解决方案2】:

鉴于收到的 error 为零,您在完成块中收到 user。所以基本上检索到的 user 的类型是可选的。

private func setUserInfo(user: User?, username: String, name: String, email: String, password: String){
    if error == nil && user != nil
     {
       self.saveUserInfo(user: user!, username: username, name: name, password: password)
      } else {

       print(error?.localizedDescription as Any)
}

【讨论】:

  • 试过了。它给了我以下信息:Cannot convert value type “User?” to expected argument type “User?”
  • 我不明白为什么它会在您回答后给出错误。这对我来说很有意义
【解决方案3】:

在 firebase 完成处理程序中,用户对象是可选的,但在您的方法中,您期望的是非可选的用户对象。你可以像这样修复它:

// Sign Up Function
func signUp(username: String, name: String, email: String, password: String
{
    Auth.auth().createUser(withEmail: email, password: password, completion: { (user, error) in
        if error == nil && user != nil
        {
            // error happens here
            self.setUserInfo(user: user!, username: username, name: name, email: email, password: password)

        }
        else
        {
            print(error?.localizedDescription as Any)
        }
    })
}

// Set the user info to Firebase storage. Profile picture change, username, and password
private func setUserInfo(user: User, username: String, name: String, email: String, password: String)
{
    if error == nil
    {
        self.saveUserInfo(user: user, username: username, name: name, password: password)
    }
    else
    {
        print(error?.localizedDescription as Any)
    }
}

【讨论】:

  • 当我添加 ! 时,它返回以下内容:Cannot convert value type “User” to expected argument type “User!”
  • @BrunoRecillas:你能检查更新的代码吗?
  • 我得到以下信息:Cannot convert value type “User?” to expected argument type “User”
  • @BrunoRecillas:您在哪一行得到错误?同一行?您使用的是我添加的上述代码吗?
  • 是的,同一行。错误如下Cannot convert value type “User” to expected argument type “User”
猜你喜欢
  • 1970-01-01
  • 2019-10-18
  • 2018-04-04
  • 1970-01-01
  • 1970-01-01
  • 2021-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多