【问题标题】:Cannot convert value of type 'User?' to expected argument type 'User!'无法转换“用户?”类型的值到预期的参数类型“用户!”
【发布时间】:2017-10-19 01:37:35
【问题描述】:

我怀疑我的代码有问题,我无法确定我在这里做错了什么。错误在此行:self.setUserInfo(firstLastName: firstLastName, user: user, username: username, location: location, biography: biography, password: password, pictureData: pictureData) 在我的注册函数中。

无法转换“用户?”类型的值到预期的参数类型“用户!”

func signUP(firstLastName: String, username: String, email: String, location: String, biography: String, password: String, pictureData: NSData!) {

    Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
        if error == nil{

           self.setUserInfo(firstLastName: firstLastName, user: user, username: username, location: location, biography: biography, password: password, pictureData: pictureData)

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


}

private func setUserInfo(firstLastName: String, user: User!, username: String, location: String, biography: String, password: String, pictureData: NSData!){

    let imagePath = "profileImage\(user.uid)/userPic.jpg"
    let imageRef = storageRef.child(imagePath)

let metaData = StorageMetadata()
    metaData.contentType = "image/jpeg"
    imageRef.putData(pictureData as Data, metadata: metaData){(newMetaData, error)
        in
        if error == nil{
            let changeRequest = User.createProfileChangeRequest()
            changeRequest.displayName = username
            if let photoURL = newMetaData!.downloadURL(){
                changeRequest.photoURL = photoURL

            }
            changeRequest.commitChanges(completion: { (error) in
                if error == nil{

                    self.saveUserInfo(firstLastName: firstLastName, user: user, username: username, location: location, biography: biography, password: password)

                    print("user info set")

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

        }else{
            print(error?.localizedDescription)
        }

    }

}

【问题讨论】:

  • 显示Auth.auth().createUser(withEmail:方法的实现
  • 你的用户类是什么样的?

标签: swift firebase firebase-authentication optional


【解决方案1】:

在 setUserInfo 方法中,您使用“!”设置了用户参数的展开值,但在您的身份验证中。完成块似乎该值是可选的。

解决方法如下:

Auth.auth().createUser(withEmail: email, password: password) { [weak self] (user, error) in

    guard let unwrappedError = error, 
          let unwrappedUser = user, 
          let strongSelf = self 
    else {
        print(error?.localizedDescription)
        return
    }

    strongSelf.setUserInfo(firstLastName: firstLastName, user: unwrappedUser, username: username, location: location, biography: biography, password: password, pictureData: pictureData)
}

编辑:我添加了一个 strongSelf 模式并使用了一个保护语句,因为你在这里明确地保留了 self (网络回调闭包中的一个 no no !),并且在这种情况下,保护语句是处理有效性检查的首选方法,因为它更清楚你的意图是什么。

【讨论】:

  • 删除“!”从 setUserInfo 声明 User 参数后
  • 这带来了更多错误。 (提供图片)
  • 哦,但是你是如何定义你的类的那些方法声明的?所以我们将修复第一个错误。对于第二个错误,我在 guard let 语句中做了一些更改。至于第三个,我看你没有去掉“!”从您的方法声明 private func setUserInfo(firstLastName: String, user: User!, username: String, location: String, biography: String, password: String, pictureData: NSData!)。它仍然存在
猜你喜欢
  • 2018-01-07
  • 2019-10-18
  • 2018-04-04
  • 1970-01-01
  • 1970-01-01
  • 2021-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多