Xcode Firebase | Cannot convert value of type'

2020-05-03 13:09发布

I tried literally tried every possible option to write it, but I can't seem to figure out the solution.

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")
    }
}

2条回答
▲ chillily
2楼-- · 2020-05-03 13:47

Starting from Firebase API 5.0 it's createUser() method returns FIRAuthDataResultCallback instead of User object directly.

In your case you can fix it my making following changes:

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)
            }
        })
    }

For more code readability I would replace your code like below:

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)
            }
        })
    }
查看更多
虎瘦雄心在
3楼-- · 2020-05-03 13:53

You need

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

func createProfile(_ user: FIRUser ) { --- }
查看更多
登录 后发表回答