简体   繁体   English

使用“执行 segue”传递错误返回 nil?

[英]Passing error with “perform segue” returns nil?

I am trying to show app users the error when they login in or register in a wrong way, for example the password needs 6 characters so if he/she entered 4 or 5 its should bring him error but it only prints in Xcode and always nil in the app (I am new here and the website tells me the picture size is too big to be uploaded so ill write down).我试图在应用程序用户以错误的方式登录或注册时向他们显示错误,例如密码需要 6 个字符,所以如果他/她输入 4 或 5,它应该会给他带来错误,但它只会在 Xcode 中打印并且始终为零在应用程序中(我是新来的,网站告诉我图片太大而无法上传,所以我记下来了)。 Can someone help me figure what's wrong with the code here?有人可以帮我弄清楚这里的代码有什么问题吗? Thanks!!谢谢!!

// 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"

The reason is that prepare(for segue must be implemented on the top level of the class (same level as viewDidLoad ).原因是prepare(for segue必须在 class 的顶层实现(与viewDidLoad相同的级别)。

And please name variables, functions and enum cases with starting lowercase letter.并且请以小写字母开头命名变量、函数和枚举案例。

func prepare(for segue: UIStoryboardSegue, sender: Any?)

This function is called every time segue occurs (when performSegue(withIndentifier:sender) function calls).每次发生 segue 时都会调用此 function(当 performSegue(withIndentifier:sender) function 调用时)。

viewController has no access to prepare() function because it is in local scope of if/else block. viewController 无权访问 prepare() function,因为它位于 if/else 块的本地 scope 中。

More specifically, prepare(for segue) function exists only in scope of if/else block, that is wrapped in a closure, that is wrapped in another if/else block.更具体地说,prepare(for segue) function 仅存在于 if/else 块的 scope 中,它被包裹在一个闭包中,该闭包被包裹在另一个 if/else 块中。

But this function should be always accessible for viewController, that why it should be placed in a top level scope.但是这个 function 应该始终可供 viewController 访问,这就是为什么它应该放在顶层 scope 中。

Like that像那样

class MyViewController: UIViewController {

    //other class properties and methods

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        //your code here
    }
}

I would suggest to read more about scope & context in swift.我建议在 swift 中阅读更多关于 scope 和上下文的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM