Xcode Firebase |Невозможно преобразовать значение типа ʻAuthDataResult 'в ожидаемый тип аргумента' User ' - PullRequest
0 голосов
/ 15 февраля 2019

Я пытался буквально попробовать все возможные варианты, чтобы написать его, но я не могу найти решение.

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 ]

0 голосов
/ 15 февраля 2019

Начиная с Firebase API 5.0, метод createUser () возвращает FIRAuthDataResultCallback вместо Пользователь объект напрямую.

В вашем случае вы можете исправить это, внеся следующие изменения:

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)
            }
        })
    }
0 голосов
/ 15 февраля 2019

Вам нужно

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

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