类型"AuthDataResult"的值没有成员"uid" [英] Value of type 'AuthDataResult' has no member 'uid'

查看:43
本文介绍了类型"AuthDataResult"的值没有成员"uid"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是菜鸟,正在尝试通过Youtube视频创建社交应用,并且我正在按照该视频的说明进行操作.

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

大约3分钟.视频中,您将看到代码.

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

这是我的代码.

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

错误的屏幕截图

我在这里做错了什么以及如何解决?

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

推荐答案

尝试与Guard语句一起使用,以安全地解开可选内容.试试看!

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)

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

希望对您有帮助.

这篇关于类型"AuthDataResult"的值没有成员"uid"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆