-2

I know there are tons of posts on the internet about this, I also know what this error message means, I have encountered it at least 50 times. Although I have tried for about 1 hour and I don't know why I am getting this error on this code:

        usernameSignUp?.setPadding()
        emailSignUp?.setPadding()
        passwordSignUp?.setPadding()
        usernameSignUp?.setBottomBorder()
        emailSignUp?.setBottomBorder()
        passwordSignUp?.setBottomBorder()
        emailSignIn?.setPadding()
        emailSignIn?.setBottomBorder()
        passwordSignIn?.setPadding()
        passwordSignIn?.setBottomBorder()
        
        signUpButton?.layer.cornerRadius = 4
        
    
    }
    
    func validateFields() -> String? {
        
        if usernameSignUp.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
            emailSignUp.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
            passwordSignUp.text?.trimmingCharacters(in: .whitespacesAndNewlines) == ""{
            
            
            return "Please fill in all fields"
        }
        
        let cleanedPassword = passwordSignUp.text!.trimmingCharacters(in: .whitespacesAndNewlines)
        
        if isPasswordValid(cleanedPassword) == false {
            
            return "8 characters or more"
            
        }
        
        
        return nil
    }
    
  
    @IBAction func signUpTapped(_ sender: Any) {
        
        let error = validateFields()
              
              if error != nil {
                  
                  showError(error!)
    }
              
              else {
                
                let userName = usernameSignUp.text!.trimmingCharacters(in: .whitespacesAndNewlines)
                let email = emailSignUp.text!.trimmingCharacters(in: .whitespacesAndNewlines)
                let password = passwordSignUp.text!.trimmingCharacters(in: .whitespacesAndNewlines)
                
                
                Auth.auth().createUser(withEmail: email, password: password) { (result, error) in
                    
                    if error != nil {
                        
                        self.showError("Invalid email")
                        
                        
                    }
                    else {
                        
                        let db = Firestore.firestore()
                        
                        db.collection("users").addDocument(data: ["username": userName, "uid": result!.user.uid]) { (error) in
                            
                            if error != nil {
                                
                                self.showError("Username could not be created")
                                
                            }
                            
                            
                        }
                        
                        self.transitionToHome()
                        
                        
                    }
                    
                    
                }
                
        }
    
        
    }
    
     func isPasswordValid(_ password : String) -> Bool {
        
        let passwordTest = NSPredicate(format: "SELF MATCHES %@", ".{8,}")
        
        return passwordTest.evaluate(with: password)
    }
    
    func showError(_ message:String) {
        
        errorLabel.text = message
        errorLabel.alpha = 1
        
    }
    
    func transitionToHome() {
        
        let homeViewController = storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? ViewController
        
        view.window?.rootViewController = homeViewController
        
        view.window?.makeKeyAndVisible()
        
        
    }
    
    
    
    @IBAction func emailLoginTapped(_ sender: Any) {
        
        
    }
    
    
    
    @IBAction func loginTapped(_ sender: Any) {
        
        
        if let emailsignin = emailSignIn.text?.trimmingCharacters(in: .whitespacesAndNewlines),

            let passwordsignin = passwordSignIn.text?.trimmingCharacters(in: .whitespacesAndNewlines) {
        
        Auth.auth().signIn(withEmail: emailsignin, password: passwordsignin) { (result, error) in
          
            
            if error != nil {
            
                self.errorLabel3.text = error!.localizedDescription
                self.errorLabel3.alpha = 1
                
            
            
        }
            else {
                
                
                self.transitionToHome()
                
            }
        
        
    }
    
    
    
    
    
    
    

}
}
}

It is happening in the first two lines of code. If anyone could help it would be greatly appreciated.

  • 1
    If you know what the error message means, then you understand that the `.text` property of either `emailSignIn` or `paasswordSignIn` is `nil`. How do you expect us to help you figure out *why*, without seeing how you assigned a value to them? – New Dev Jul 02 '20 at 01:37
  • 1
    Learn to about optionals, or your Swift journey will consist of nothing but fighting them and suffering – Alexander Jul 02 '20 at 01:37
  • @NewDev what do you mean how I assigned a value to them? – appledeveloper123 Jul 02 '20 at 01:41
  • @appledeveloper123 - presumably, you expect `emailSignIn.text` and/or `passwordSignIn.text` not to be `nil`, so somewhere this property should have been assigned a value, but wasn't. It's not in the code you showed. So, in other words, your bug is not reproducible with the code you posted in the question. – New Dev Jul 02 '20 at 01:45
  • @NewDev I have a bunch of other code and when it really comes down to it I am just trying to make my first sign up and sign in page. `emailSignIn` and `passwordSignIn` are both just text fields where the user enters to sign in. I guess my question is how do I prevent this from being nil? – appledeveloper123 Jul 02 '20 at 01:54
  • Read about [optional binding](https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html#ID333) - you can ensure that both are not `nil` before you perform some action that relies on these values. – New Dev Jul 02 '20 at 01:58

1 Answers1

0

If you need to ensure that the values are non-nil before you use them, use optional binding to ensure that and optionally extract their non-nil value:

if let emailText = emailSignIn.text?.trimmingCharacters(in: .whitespacesAndNewlines), 
   let passwordText = passwordSignIn.text?.trimmingCharacters(in: .whitespacesAndNewlines) {

   Auth.auth().signIn(withEmail: emailText, password: passwordText) { (result, err) in
      // etc...
   }
}
New Dev
  • 46,536
  • 12
  • 79
  • 122