'AuthDataResult'类型的值没有成员'uid'

时间:2018-05-16 16:56:40

标签: firebase swift4 xcode9

我是一名尝试通过Youtube视频创建社交应用的菜鸟,我正在按照此视频的说明进行操作。

https://youtu.be/GrRggN41VF0

大约3分钟。您将看到该视频的视频。

这是我的代码。

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

我在这里做错了什么,如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

尝试使用:

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

答案 1 :(得分:0)

尝试使用guard语句安全地打开选项。试一试!

ToySmallImpl

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

我希望这可以帮助你......