首页 > 解决方案 > 使用“执行segue”传递错误返回nil?

问题描述

我试图在应用程序用户以错误的方式登录或注册时向他们显示错误,例如密码需要 6 个字符,所以如果他/她输入 4 或 5,它应该会给他带来错误,但它只在 Xcode 中打印并且总是 nil在应用程序中(我是新来的,网站告诉我图片太大而无法上传,所以我记下来了)。有人可以帮我弄清楚这里的代码有什么问题吗?谢谢!!

// This is the code for the first viewcontroller where I try to pass the error to the ErrorViewController

 if let email = emailTextfield.text, let password = passwordTextfield.text{
    Auth.auth().signIn(withEmail: email, password: password) { [weak self] authResult, error in 
     if let e = error {
        print(e)
        self!.performSegue(withIdentifier: "GoToError", sender: self)
       func prepare(for segue: UIStoryboardSegue, sender: Any?) {
       if segue.identifier == "GoToError" {
           let GoTo = segue.destination as! ErrorViewController
        GoTo.errorText = "\(e.localizedDescription)"}
       }
     }else{
        self!.performSegue(withIdentifier:K.loginSegue, sender: self)
        }

    }
    }

// This is the code of the ErrorViewController where It should catch the error and pass it to the ErrorText label and get printed to the app screen 

errorText: String?   

@IBOutlet weak var ViewMessage: UIView!
@IBOutlet weak var ErrorText: UILabel!
override func viewDidLoad() {
    super.viewDidLoad()

     ViewMessage.layer.cornerRadius = ViewMessage.frame.size.height / 4



    if errorText != nil {
    ErrorText.text = errorText
    }else{

// The string below is what gets printed because the error is always nil

        ErrorText.text = "make sure your password is at least 6 characters"

标签: iosswiftxcodeerror-handlingsegue

解决方案


原因是prepare(for segue必须在类的顶层(与 同级viewDidLoad)上实现。

并且请以小写字母开头命名变量、函数和枚举案例。


推荐阅读