Value of type 'AuthDataResult' has no memb

2019-04-16 08:58发布

问题:

I am a noob trying to create a social app through Youtube videos, and I am following instructions of this video.

https://youtu.be/GrRggN41VF0

At around 3 mins. of the video you'll see the code.

Here's the code I have.

import UIKit
import Firebase
import SwiftKeychainWrapper

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var userImgView: UIImageView!

    @IBOutlet weak var usernameField: UITextField!

    @IBOutlet weak var emailField: UITextField!

    @IBOutlet weak var passwordField: UITextField!

    override func viewDidLoad() {

        emailField.delegate = self
        passwordField.delegate = self

        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func viewDidAppear(_ animated: Bool) {
        if let _ = KeychainWrapper.standard.string(forKey: "uid") {
            self.performSegue(withIdentifier: "toFeed", sender: nil)
        }
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {

        if textField == emailField {
            passwordField.becomeFirstResponder()
        } else if textField == passwordField {
            textField.resignFirstResponder()
        }
        return true
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func signInPressed(_ sender: Any) {
        if let email = emailField.text, let password = passwordField.text {
            Auth.auth().signIn(withEmail: email, password: password) { (user, error) in
                if error != nil {
                    // Create Account
                } else {
                    if let userID = user?.uid {
                        KeychainWrapper.standard.set((userID), forKey: "uid")
                        self.performSegue(withIdentifier: "toFeed", sender: nil)
                    }
                }
            }
        }
    }
}

Screenshot of the error

What am I doing wrong here and how do I fix this?

回答1:

Try using:

if let user = Auth.auth().currentUser, let uid = user?.uid {
                    KeychainWrapper.standard.set((uid), forKey: "uid")
                    self.performSegue(withIdentifier: "toFeed", sender: nil)
                }


回答2:

Try using with the guard statement to unwrap optionals safely. Give it a try!

guard let uid = user?.uid else { return }
KeychainWrapper.standard.set((uid), forKey: "uid")
self.performSegue(withIdentifier: "toFeed", sender: nil)

or

guard let uid = Auth.auth().currentUser?.uid else { return }
KeychainWrapper.standard.set((uid), forKey: "uid")
self.performSegue(withIdentifier: "toFeed", sender: nil)

I hope this helps you..